agora inbox for [email protected]help / color / mirror / Atom feed
New array functions 280+ messages / 6 participants [nested] [flat]
* New array functions @ 2003-08-28 17:13 Greg Stark <[email protected]> 0 siblings, 1 reply; 280+ messages in thread From: Greg Stark @ 2003-08-28 17:13 UTC (permalink / raw) To: pgsql-hackers So where are the new array functions and syntaces documented? Specifically I want to know how to replace my int_array_aggregate(int) and int_array_enum(_int) calls. And how to replace my "arr *= n" calls too. I think these are supposed be "ALL my_array" and "n = ANY myarray" or something like that? -- greg ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 18:15 Joe Conway <[email protected]> parent: Greg Stark <[email protected]> 0 siblings, 4 replies; 280+ messages in thread From: Joe Conway @ 2003-08-28 18:15 UTC (permalink / raw) To: Greg Stark <[email protected]>; +Cc: pgsql-hackers Greg Stark wrote: > So where are the new array functions and syntaces documented? Mainly here: http://developer.postgresql.org/docs/postgres/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS http://developer.postgresql.org/docs/postgres/arrays.html http://developer.postgresql.org/docs/postgres/functions-array.html http://developer.postgresql.org/docs/postgres/functions-comparisons.html#AEN12154 > Specifically I want to know how to replace my int_array_aggregate(int) and > int_array_enum(_int) calls. I have no idea what those are -- are they from contrib? You can create an aggregate to turn arbitrary datatype elements into arrays like this: CREATE AGGREGATE array_aggregate ( BASETYPE = anyelement, SFUNC = array_append, STYPE = anyarray, INITCOND = '{}' ); -- silly example, but what the heck ;-) regression=# select attrelid, array_aggregate(attnum) from pg_attribute where attnum > 0 and attnum < 5 group by attrelid limit 3; attrelid | array_aggregate ----------+----------------- 16639 | {1} 16638 | {1} 17022 | {1,2,3,4} (3 rows) If int_array_enum() is supposed to take '{1,2,3}' and produce three rows, that function was proposed but rejected. Subsequently Peter Eisentraut pointed out a SQL99 syntax that does this, but I did not get it done for 7.4. Perhaps for 7.5. > And how to replace my "arr *= n" calls too. See: http://developer.postgresql.org/docs/postgres/functions-comparisons.html#AEN12154 regression=# SELECT g.grosysid, g.groname, s.usesysid, s.usename FROM pg_shadow s, pg_group g WHERE s.usesysid = any (g.grolist); grosysid | groname | usesysid | usename ----------+---------+----------+---------- 102 | admins | 1 | postgres 100 | grp1 | 100 | user1 101 | grp2 | 100 | user1 100 | grp1 | 101 | user2 100 | grp1 | 102 | user3 101 | grp2 | 102 | user3 102 | admins | 103 | john (7 rows) HTH, Joe ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 18:45 Greg Stark <[email protected]> parent: Joe Conway <[email protected]> 3 siblings, 0 replies; 280+ messages in thread From: Greg Stark @ 2003-08-28 18:45 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Joe Conway <[email protected]> writes: > Greg Stark wrote: > > So where are the new array functions and syntaces documented? > > Mainly here: > http://developer.postgresql.org/docs/postgres/arrays.html excellent. thank you. > > Specifically I want to know how to replace my int_array_aggregate(int) and > > int_array_enum(_int) calls. > > I have no idea what those are -- are they from contrib? indeed in contrib/intagg > You can create an aggregate to turn arbitrary datatype elements into arrays > like this: > > CREATE AGGREGATE array_aggregate > ( > BASETYPE = anyelement, > SFUNC = array_append, > STYPE = anyarray, > INITCOND = '{}' > ); Hm, perhaps there should be a standard name for this, rather than have everyone's code do their own thing. > If int_array_enum() is supposed to take '{1,2,3}' and produce three rows, that > function was proposed but rejected. Subsequently Peter Eisentraut pointed out a > SQL99 syntax that does this, but I did not get it done for 7.4. Perhaps for 7.5. That's exactly what it does. Hm, I guess I misinterpreted that post. Hm I have some work to do. Thanks. And thanks a LOT for doing the work, it'll makes a big difference and make arrays much more practical to use. -- greg ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 19:13 Tom Lane <[email protected]> parent: Joe Conway <[email protected]> 3 siblings, 1 reply; 280+ messages in thread From: Tom Lane @ 2003-08-28 19:13 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Joe Conway <[email protected]> writes: > Greg Stark wrote: >> And how to replace my "arr *= n" calls too. > See: > http://developer.postgresql.org/docs/postgres/functions-comparisons.html#AEN12154 That reminds me --- contrib/array is definitely obsolete now, and there may be parts of the other contrib array- and aggregate-related modules that are obsoleted by Joe's recent work. I would like to kill contrib/array for 7.4, because it's one of the GPL'd contrib modules that I was tasked to get rid of some time ago. What I'm thinking of doing is removing the code, and replacing the README with a note explaining how to convert contrib/array queries to use the new mainstream syntaxes. That will give contrib/array users a clue what they're supposed to do. In a release or three the README could go away too. Comments, objections? Also, does anyone want to look for possible dead code in intagg and so on? regards, tom lane ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 20:16 Greg Stark <[email protected]> parent: Joe Conway <[email protected]> 3 siblings, 1 reply; 280+ messages in thread From: Greg Stark @ 2003-08-28 20:16 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers > See: > http://developer.postgresql.org/docs/postgres/functions-comparisons.html#AEN12154 > > regression=# SELECT g.grosysid, g.groname, s.usesysid, s.usename FROM pg_shadow > s, pg_group g WHERE s.usesysid = any (g.grolist); These forms below are all equivalent, right? If so ideally they would all be converted to an equivalent form and therefore produce the same plan. I guess I'm wishing for a pony though. But I think currently I'm stuck with the worst of these and I don't see any way of escaping to the better plans. Incidentally, "HashAggregate"?! Based on the earlier discussion on this I would have expected that line to read "Materialize" slo=> explain select * from store_location where store_location_id in (1,2,3); QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------- Index Scan using store_location_pkey, store_location_pkey, store_location_pkey on store_location (cost=0.00..17.74 rows=3 width=523) Index Cond: ((store_location_id = 1) OR (store_location_id = 2) OR (store_location_id = 3)) (2 rows) slo=> explain select * from store_location where store_location_id in (select 1 union all select 2 union all select 3); QUERY PLAN -------------------------------------------------------------------------------------------------- Nested Loop (cost=0.10..17.86 rows=3 width=523) -> HashAggregate (cost=0.10..0.10 rows=3 width=4) -> Subquery Scan "IN_subquery" (cost=0.00..0.09 rows=3 width=4) -> Append (cost=0.00..0.06 rows=3 width=0) -> Subquery Scan "*SELECT* 1" (cost=0.00..0.02 rows=1 width=0) -> Result (cost=0.00..0.01 rows=1 width=0) -> Subquery Scan "*SELECT* 2" (cost=0.00..0.02 rows=1 width=0) -> Result (cost=0.00..0.01 rows=1 width=0) -> Subquery Scan "*SELECT* 3" (cost=0.00..0.02 rows=1 width=0) -> Result (cost=0.00..0.01 rows=1 width=0) -> Index Scan using store_location_pkey on store_location (cost=0.00..5.91 rows=1 width=523) Index Cond: (store_location.store_location_id = "outer"."?column?") (12 rows) slo=> explain select * from store_location where store_location_id = any (array[1,2,3]); QUERY PLAN --------------------------------------------------------------------- Seq Scan on store_location (cost=0.00..825.75 rows=5954 width=523) Filter: (store_location_id = ANY ('{1,2,3}'::integer[])) (2 rows) -- greg ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 20:51 Joe Conway <[email protected]> parent: Tom Lane <[email protected]> 0 siblings, 1 reply; 280+ messages in thread From: Joe Conway @ 2003-08-28 20:51 UTC (permalink / raw) To: Tom Lane <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Tom Lane wrote: > What I'm thinking of doing is removing the code, and replacing the > README with a note explaining how to convert contrib/array queries to > use the new mainstream syntaxes. That will give contrib/array users > a clue what they're supposed to do. In a release or three the README > could go away too. I have no objection to removing it now, but previously I think you agreed with Bruce's comment that we should leave it intact (but deprecated) for 7.4, and remove in 7.5. > Also, does anyone want to look for possible dead code in intagg and > so on? I did a quick review back in July. IIRC, intagg could be functionally replaced with the aggregate definition that I posted, except that intagg is probably a fair bit better performance (I didn't actually test), in that it accumulates the array in backend memory and just pushes pointers around as int4's. I've thought that a safer implementation would be needed to fold it into the backend (maybe using hashes keyed with the pointer?), but in any case that's a 7.5 thing. Also IIRC there were some functions in intarray that overlap the new backend functionality, but much of it (i.e. using GIST to index into large arrays) is not. I'll try to review them again and make a recommendation in the next couple of days, but it might be a stretch because I'm trying to tie up lots of loose ends in preparation for a trip next week. Joe ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 20:52 Hannu Krosing <[email protected]> parent: Joe Conway <[email protected]> 3 siblings, 1 reply; 280+ messages in thread From: Hannu Krosing @ 2003-08-28 20:52 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Joe Conway kirjutas N, 28.08.2003 kell 21:15: > Greg Stark wrote: > > Specifically I want to know how to replace my int_array_aggregate(int) and > > int_array_enum(_int) calls. > > I have no idea what those are -- are they from contrib? > > You can create an aggregate to turn arbitrary datatype elements into > arrays like this: > > CREATE AGGREGATE array_aggregate > ( > BASETYPE = anyelement, > SFUNC = array_append, > STYPE = anyarray, > INITCOND = '{}' > ); Any idea of performance - is this array_aggregate(anyelement) faster, slower or about same than int_array_aggregate(int) ? > If int_array_enum() is supposed to take '{1,2,3}' and produce three > rows, that function was proposed but rejected. Subsequently Peter > Eisentraut pointed out a SQL99 syntax that does this, but I did not get > it done for 7.4. Perhaps for 7.5. So we got to keep intagg at least until 7.5 ... ----------- Hannu ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 21:05 Tom Lane <[email protected]> parent: Joe Conway <[email protected]> 0 siblings, 1 reply; 280+ messages in thread From: Tom Lane @ 2003-08-28 21:05 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Joe Conway <[email protected]> writes: > I have no objection to removing it now, but previously I think you > agreed with Bruce's comment that we should leave it intact (but > deprecated) for 7.4, and remove in 7.5. Did we discuss this already? I'd forgotten. In any case, the module isn't visibly deprecated at the moment. If the idea is to avoid blindsiding its users, then we definitely must mark it as slated for removal, and provide some docs about how to replace it. regards, tom lane ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 21:08 Joe Conway <[email protected]> parent: Hannu Krosing <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Joe Conway @ 2003-08-28 21:08 UTC (permalink / raw) To: Hannu Krosing <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Hannu Krosing wrote: > Any idea of performance - is this array_aggregate(anyelement) faster, > slower or about same than int_array_aggregate(int) ? I haven't tested, but I'd guess for an array of any significant length int_array_aggregate() is faster (see my other post). That's one of the reasons I haven't advocated deprecating intagg yet. Joe ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 21:44 Joe Conway <[email protected]> parent: Tom Lane <[email protected]> 0 siblings, 1 reply; 280+ messages in thread From: Joe Conway @ 2003-08-28 21:44 UTC (permalink / raw) To: Tom Lane <[email protected]>; +Cc: Greg Stark <[email protected]>; pgsql-hackers Tom Lane wrote: > Joe Conway <[email protected]> writes: > >>I have no objection to removing it now, but previously I think you >>agreed with Bruce's comment that we should leave it intact (but >>deprecated) for 7.4, and remove in 7.5. > > Did we discuss this already? I'd forgotten. > > In any case, the module isn't visibly deprecated at the moment. > If the idea is to avoid blindsiding its users, then we definitely > must mark it as slated for removal, and provide some docs about > how to replace it. > I can't find it in the archives for some reason, but here was the exchange: Tom Lane wrote: > Bruce Momjian <[email protected]> writes: > >>Joe Conway wrote: >> >>>I do agree that it makes contrib/array unnecessary. I was going to >>>suggest we remove that if this was committed. > >>Good idea. > > We could do that, but it might be more friendly to just mark it as > deprecated for one release cycle before zapping it. That'd give > people who use it some time to convert over. So I guess since it was actually you who objected, you have the right to change your mind ;-) Joe ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: New array functions @ 2003-08-28 23:04 Tom Lane <[email protected]> parent: Greg Stark <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Tom Lane @ 2003-08-28 23:04 UTC (permalink / raw) To: Greg Stark <[email protected]>; +Cc: Joe Conway <[email protected]>; pgsql-hackers Greg Stark <[email protected]> writes: > Incidentally, "HashAggregate"?! Based on the earlier discussion on this I > would have expected that line to read "Materialize" It's using a grouped aggregation node to implement a UNIQUE filter, so that it can replace the "WHERE foo IN (subselect)" by a straight join. Of course in this case the uniqueness filter is a waste of time, but in general the planner can't be expected to know that. regards, tom lane ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: [HACKERS] New array functions @ 2003-08-31 04:42 Joe Conway <[email protected]> parent: Joe Conway <[email protected]> 0 siblings, 2 replies; 280+ messages in thread From: Joe Conway @ 2003-08-31 04:42 UTC (permalink / raw) To: ; +Cc: Tom Lane <[email protected]>; Greg Stark <[email protected]>; Patches (PostgreSQL) <[email protected]> Joe Conway wrote: > Tom Lane wrote: >> Did we discuss this already? I'd forgotten. >> > I can't find it in the archives for some reason, but here was the exchange: > > Tom Lane wrote: > > Bruce Momjian <[email protected]> writes: > > > >>Joe Conway wrote: > >> > >>>I do agree that it makes contrib/array unnecessary. I was going to > >>>suggest we remove that if this was committed. > > > >>Good idea. > > > > We could do that, but it might be more friendly to just mark it as > > deprecated for one release cycle before zapping it. That'd give > > people who use it some time to convert over. > > So I guess since it was actually you who objected, you have the right to > change your mind ;-) > Here is a patch that removes contrib/array, leaving only the README with some examples of the new syntax and a reference to the documentation. I'll try to take a look at contrib/intarray and contrib/intagg before the weekend is through, and at least post a recommendation. Joe Index: contrib/array/Makefile =================================================================== RCS file: contrib/array/Makefile diff -N contrib/array/Makefile *** contrib/array/Makefile 6 Sep 2001 10:49:29 -0000 1.16 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,11 **** - # $Header: /opt/src/cvs/pgsql-server/contrib/array/Makefile,v 1.16 2001/09/06 10:49:29 petere Exp $ - - subdir = contrib/array - top_builddir = ../.. - include $(top_builddir)/src/Makefile.global - - MODULES = array_iterator - DATA_built = array_iterator.sql - DOCS = README.array_iterator - - include $(top_srcdir)/contrib/contrib-global.mk --- 0 ---- Index: contrib/array/README.array_iterator =================================================================== RCS file: /opt/src/cvs/pgsql-server/contrib/array/README.array_iterator,v retrieving revision 1.2 diff -c -r1.2 README.array_iterator *** contrib/array/README.array_iterator 26 Aug 2002 17:53:57 -0000 1.2 --- contrib/array/README.array_iterator 31 Aug 2003 04:37:04 -0000 *************** *** 1,49 **** ! Array iterator functions, by Massimo Dal Zotto <[email protected]> ! Copyright (C) 1999, Massimo Dal Zotto <[email protected]> ! This software is distributed under the GNU General Public License ! either version 2, or (at your option) any later version. ! This loadable module defines a new class of functions which take ! an array and a scalar value, iterate a scalar operator over the ! elements of the array and the value, and compute a result as ! the logical OR or AND of the iteration results. ! For example array_int4eq returns true if some of the elements ! of an array of int4 is equal to the given value: ! ! array_int4eq({1,2,3}, 1) --> true ! array_int4eq({1,2,3}, 4) --> false ! ! If we have defined T array types and O scalar operators we can ! define T x O x 2 array functions, each of them has a name like ! "array_[all_]<basetype><operation>" and takes an array of type T ! iterating the operator O over all the elements. Note however ! that some of the possible combination are invalid, for example ! the array_int4_like because there is no like operator for int4. ! ! We can then define new operators based on these functions and use ! them to write queries with qualification clauses based on the ! values of some of the elements of an array. ! For example to select rows having some or all element of an array ! attribute equal to a given value or matching a regular expression: ! ! create table t(id int4[], txt text[]); ! ! -- select tuples with some id element equal to 123 ! select * from t where t.id *= 123; ! ! -- select tuples with some txt element matching '[a-z]' ! select * from t where t.txt *~ '[a-z]'; ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where t.txt[1:3] **~ '^[A-Z]'; ! ! The scheme is quite general, each operator which operates on a base type ! can be iterated over the elements of an array. It seem to work well but ! defining each new operator requires writing a different C function. ! This is tedious, and error-prone since one must take care that the correct ! datatypes are associated with the selected underlying function. ! Can anyone suggest a better and more portable way to do it ? - See also array_iterator.sql for an example on how to use this module. --- 1,32 ---- ! Array iterator functions have been removed as of PostgreSQL 7.4, because ! equivalent functionality is now available built in to the backend. ! For example, previously, using contrib/array, you might have used the ! following construct: + create table t(id int4[], txt text[]); ! -- select tuples with some id element equal to 123 ! select * from t where t.id *= 123; ! ! Now you would do this instead: ! ! -- select tuples with some id element equal to 123 ! select * from t where 123 = any (t.id); ! ! -- or you could also do this ! select * from t where 123 = some (t.id); ! ! Similarly, if using contrib/array, you did the following: ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where t.txt[1:3] **~ '^[A-Z]'; ! ! Now do this instead: ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where '^[A-Z]' ~ all (t.txt[1:3]); ! ! See the related section in the online documentation for more detail: ! Table of Contents => Functions and Operators => Row and Array Comparisons Index: contrib/array/array_iterator.c =================================================================== RCS file: contrib/array/array_iterator.c diff -N contrib/array/array_iterator.c *** contrib/array/array_iterator.c 26 May 2003 00:11:27 -0000 1.26 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,333 **** - /* - * array_iterator.c -- - * - * This file defines a new class of operators which take an - * array and a scalar value, iterate a scalar operator over the - * elements of the array and the value and compute a result as - * the logical OR or AND of the iteration results. - * - * Copyright (C) 1999, Massimo Dal Zotto <[email protected]> - * ported to postgreSQL 6.3.2,added oid_functions, 18.1.1999, - * Tobias Gabele <[email protected]> - * - * This software is distributed under the GNU General Public License - * either version 2, or (at your option) any later version. - */ - - #include "postgres.h" - - #include <ctype.h> - #include <stdio.h> - #include <sys/types.h> - #include <string.h> - - #include "access/tupmacs.h" - #include "access/xact.h" - #include "fmgr.h" - #include "miscadmin.h" - #include "utils/array.h" - #include "utils/builtins.h" - #include "utils/fmgroids.h" - #include "utils/memutils.h" - #include "utils/lsyscache.h" - - #include "array_iterator.h" - - - static int32 - array_iterator(Oid proc, int and, ArrayType *array, Datum value) - { - Oid elemtype; - int16 typlen; - bool typbyval; - char typalign; - int nitems, - i; - Datum result; - int ndim, - *dim; - char *p; - FmgrInfo finfo; - - /* Sanity checks */ - if (array == (ArrayType *) NULL) - { - /* elog(WARNING, "array_iterator: array is null"); */ - return (0); - } - - /* detoast input if necessary */ - array = DatumGetArrayTypeP(PointerGetDatum(array)); - - ndim = ARR_NDIM(array); - dim = ARR_DIMS(array); - nitems = ArrayGetNItems(ndim, dim); - if (nitems == 0) - return (0); - - /* Lookup element type information */ - elemtype = ARR_ELEMTYPE(array); - get_typlenbyvalalign(elemtype, &typlen, &typbyval, &typalign); - - /* Lookup the function entry point */ - fmgr_info(proc, &finfo); - if (finfo.fn_nargs != 2) - { - elog(ERROR, "array_iterator: proc %u does not take 2 args", proc); - return (0); - } - - /* Scan the array and apply the operator to each element */ - result = BoolGetDatum(false); - p = ARR_DATA_PTR(array); - for (i = 0; i < nitems; i++) - { - Datum itemvalue; - - itemvalue = fetch_att(p, typbyval, typlen); - - p = att_addlength(p, typlen, PointerGetDatum(p)); - p = (char *) att_align(p, typalign); - - result = FunctionCall2(&finfo, itemvalue, value); - - if (DatumGetBool(result)) - { - if (!and) - return (1); - } - else - { - if (and) - return (0); - } - } - - if (and && DatumGetBool(result)) - return (1); - else - return (0); - } - - /* - * Iterator functions for type _text - */ - - int32 - array_texteq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_texteq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTEQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_textregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_textregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 1, /* logical and */ - array, (Datum) value); - } - - /* - * Iterator functions for type _bpchar. Note that the regexp - * operators take the second argument of type text. - */ - - int32 - array_bpchareq(ArrayType *array, void *value) - { - return array_iterator(F_BPCHAREQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_bpchareq(ArrayType *array, void *value) - { - return array_iterator(F_BPCHAREQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_bpcharregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_bpcharregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 1, /* logical and */ - array, (Datum) value); - } - - /* - * Iterator functions for type _int4 - */ - - int32 - array_int4eq(ArrayType *array, int4 value) - { - return array_iterator(F_INT4EQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4eq(ArrayType *array, int4 value) - { - return array_iterator(F_INT4EQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4ne(ArrayType *array, int4 value) - { - return array_iterator(F_INT4NE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4ne(ArrayType *array, int4 value) - { - return array_iterator(F_INT4NE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4gt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GT, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4gt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GT, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4ge(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4ge(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4lt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LT, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4lt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LT, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4le(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4le(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LE, - 1, /* logical and */ - array, (Datum) value); - } - - /* new tobias gabele 1999 */ - - int32 - array_oideq(ArrayType *array, Oid value) - { - return array_iterator(F_OIDEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_oidne(ArrayType *array, Oid value) - { - return array_iterator(F_OIDNE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_ineteq(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_EQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_ineteq(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_EQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_inetne(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_NE, - 0, /* logical and */ - array, (Datum) value); - } - - int32 - array_all_inetne(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_NE, - 1, /* logical and */ - array, (Datum) value); - } --- 0 ---- Index: contrib/array/array_iterator.h =================================================================== RCS file: contrib/array/array_iterator.h diff -N contrib/array/array_iterator.h *** contrib/array/array_iterator.h 26 May 2003 00:11:27 -0000 1.10 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,38 **** - #ifndef ARRAY_ITERATOR_H - #define ARRAY_ITERATOR_H - - static int32 array_iterator(Oid proc, int and, - ArrayType *array, Datum value); - - int32 array_texteq(ArrayType *array, void *value); - int32 array_all_texteq(ArrayType *array, void *value); - int32 array_textregexeq(ArrayType *array, void *value); - int32 array_all_textregexeq(ArrayType *array, void *value); - - int32 array_bpchareq(ArrayType *array, void *value); - int32 array_all_bpchareq(ArrayType *array, void *value); - int32 array_bpcharregexeq(ArrayType *array, void *value); - int32 array_all_bpcharregexeq(ArrayType *array, void *value); - - int32 array_int4eq(ArrayType *array, int4 value); - int32 array_all_int4eq(ArrayType *array, int4 value); - int32 array_int4ne(ArrayType *array, int4 value); - int32 array_all_int4ne(ArrayType *array, int4 value); - int32 array_int4gt(ArrayType *array, int4 value); - int32 array_all_int4gt(ArrayType *array, int4 value); - int32 array_int4ge(ArrayType *array, int4 value); - int32 array_all_int4ge(ArrayType *array, int4 value); - int32 array_int4lt(ArrayType *array, int4 value); - int32 array_all_int4lt(ArrayType *array, int4 value); - int32 array_int4le(ArrayType *array, int4 value); - int32 array_all_int4le(ArrayType *array, int4 value); - - int32 array_oideq(ArrayType *array, Oid value); - int32 array_all_oidne(ArrayType *array, Oid value); - - int32 array_ineteq(ArrayType *array, void *value); - int32 array_all_ineteq(ArrayType *array, void *value); - int32 array_inetne(ArrayType *array, void *value); - int32 array_all_inetne(ArrayType *array, void *value); - - #endif --- 0 ---- Index: contrib/array/array_iterator.sql.in =================================================================== RCS file: contrib/array/array_iterator.sql.in diff -N contrib/array/array_iterator.sql.in *** contrib/array/array_iterator.sql.in 26 May 2003 00:11:27 -0000 1.10 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,329 **** - -- SQL code to define the new array iterator functions and operators - - -- define the array operators *=, **=, *~ and **~ for type _text - -- - - -- Adjust this setting to control where the objects get created. - SET search_path = public; - - CREATE OR REPLACE FUNCTION array_texteq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_texteq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_textregexeq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_textregexeq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_text,text); - CREATE OPERATOR *= ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_texteq - ); - - DROP OPERATOR **=(_text,text); - CREATE OPERATOR **= ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_all_texteq - ); - - DROP OPERATOR *~(_text,text); - CREATE OPERATOR *~ ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_textregexeq - ); - - DROP OPERATOR **~(_text,text); - CREATE OPERATOR **~ ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_all_textregexeq - ); - - - -- define the array operators *=, **=, *~ and **~ for type _bpchar - -- - CREATE OR REPLACE FUNCTION array_bpchareq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_bpchareq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_bpcharregexeq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_bpcharregexeq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_bpchar,bpchar); - CREATE OPERATOR *= ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_bpchareq - ); - - DROP OPERATOR **=(_bpchar,bpchar); - CREATE OPERATOR **= ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_all_bpchareq - ); - - DROP OPERATOR *~(_bpchar,bpchar); - CREATE OPERATOR *~ ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_bpcharregexeq - ); - - DROP OPERATOR **~(_bpchar,bpchar); - CREATE OPERATOR **~ ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_all_bpcharregexeq - ); - - - -- define the array operators *=, **=, *> and **> for type _int4 - -- - CREATE OR REPLACE FUNCTION array_int4eq(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4eq(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4ne(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4ne(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4gt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4gt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4ge(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4ge(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4lt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4lt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4le(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4le(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_int4,int4); - CREATE OPERATOR *= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4eq - ); - - DROP OPERATOR **=(_int4,int4); - CREATE OPERATOR **= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4eq - ); - - DROP OPERATOR *<>(_int4,int4); - CREATE OPERATOR *<> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4ne - ); - - DROP OPERATOR **<>(_int4,int4); - CREATE OPERATOR **<> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4ne - ); - - DROP OPERATOR *>(_int4,int4); - CREATE OPERATOR *> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4gt - ); - - DROP OPERATOR **>(_int4,int4); - CREATE OPERATOR **> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4gt - ); - - DROP OPERATOR *>=(_int4,int4); - CREATE OPERATOR *>= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4ge - ); - - DROP OPERATOR **>=(_int4,int4); - CREATE OPERATOR **>= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4ge - ); - - DROP OPERATOR *<(_int4,int4); - CREATE OPERATOR *< ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4lt - ); - - DROP OPERATOR **<(_int4,int4); - CREATE OPERATOR **< ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4lt - ); - - DROP OPERATOR *<=(_int4,int4); - CREATE OPERATOR *<= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4le - ); - - DROP OPERATOR **<=(_int4,int4); - CREATE OPERATOR **<= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4le - ); - - -- define the array operators *=, **<> for type _oid (added tobias 1. 1999) - -- - CREATE OR REPLACE FUNCTION array_oideq(_oid, oid) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_oidne(_oid, oid) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_oid,oid); - CREATE OPERATOR *= ( - LEFTARG=_oid, - RIGHTARG=oid, - PROCEDURE=array_oideq - ); - - DROP OPERATOR **<>(_oid,oid); - CREATE OPERATOR **<> ( - LEFTARG=_oid, - RIGHTARG=oid, - PROCEDURE=array_all_oidne - ); - - -- define the array operators *=, **=, *<>, **<> for type _inet - - CREATE OR REPLACE FUNCTION array_ineteq(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_ineteq(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_inetne(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_inetne(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_inet,inet); - CREATE OPERATOR *= ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_ineteq - ); - - DROP OPERATOR **=(_inet,inet); - CREATE OPERATOR **= ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_all_ineteq - ); - - DROP OPERATOR *<>(_inet,inet); - CREATE OPERATOR *<> ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_inetne - ); - - DROP OPERATOR **<>(_inet,inet); - CREATE OPERATOR **<> ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_all_inetne - ); --- 0 ---- Index: contrib/Makefile =================================================================== RCS file: /opt/src/cvs/pgsql-server/contrib/Makefile,v retrieving revision 1.45 diff -c -r1.45 Makefile *** contrib/Makefile 24 Jul 2003 16:54:58 -0000 1.45 --- contrib/Makefile 31 Aug 2003 04:47:01 -0000 *************** *** 5,11 **** include $(top_builddir)/src/Makefile.global WANTED_DIRS = \ - array \ btree_gist \ chkpass \ cube \ --- 5,10 ---- *************** *** 44,49 **** --- 43,49 ---- vacuumlo # Missing: + # array \ (removed all but the README) # adddepend \ (does not have a makefile) # ipc_check \ (does not have a makefile) # mSQL-interface \ (requires msql installed) Attachments: [text/plain] contrib-array-remove.1.patch (22.0K, ../../[email protected]/2-contrib-array-remove.1.patch) download | inline diff: Index: contrib/array/Makefile =================================================================== RCS file: contrib/array/Makefile diff -N contrib/array/Makefile *** contrib/array/Makefile 6 Sep 2001 10:49:29 -0000 1.16 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,11 **** - # $Header: /opt/src/cvs/pgsql-server/contrib/array/Makefile,v 1.16 2001/09/06 10:49:29 petere Exp $ - - subdir = contrib/array - top_builddir = ../.. - include $(top_builddir)/src/Makefile.global - - MODULES = array_iterator - DATA_built = array_iterator.sql - DOCS = README.array_iterator - - include $(top_srcdir)/contrib/contrib-global.mk --- 0 ---- Index: contrib/array/README.array_iterator =================================================================== RCS file: /opt/src/cvs/pgsql-server/contrib/array/README.array_iterator,v retrieving revision 1.2 diff -c -r1.2 README.array_iterator *** contrib/array/README.array_iterator 26 Aug 2002 17:53:57 -0000 1.2 --- contrib/array/README.array_iterator 31 Aug 2003 04:37:04 -0000 *************** *** 1,49 **** ! Array iterator functions, by Massimo Dal Zotto <[email protected]> ! Copyright (C) 1999, Massimo Dal Zotto <[email protected]> ! This software is distributed under the GNU General Public License ! either version 2, or (at your option) any later version. ! This loadable module defines a new class of functions which take ! an array and a scalar value, iterate a scalar operator over the ! elements of the array and the value, and compute a result as ! the logical OR or AND of the iteration results. ! For example array_int4eq returns true if some of the elements ! of an array of int4 is equal to the given value: ! ! array_int4eq({1,2,3}, 1) --> true ! array_int4eq({1,2,3}, 4) --> false ! ! If we have defined T array types and O scalar operators we can ! define T x O x 2 array functions, each of them has a name like ! "array_[all_]<basetype><operation>" and takes an array of type T ! iterating the operator O over all the elements. Note however ! that some of the possible combination are invalid, for example ! the array_int4_like because there is no like operator for int4. ! ! We can then define new operators based on these functions and use ! them to write queries with qualification clauses based on the ! values of some of the elements of an array. ! For example to select rows having some or all element of an array ! attribute equal to a given value or matching a regular expression: ! ! create table t(id int4[], txt text[]); ! ! -- select tuples with some id element equal to 123 ! select * from t where t.id *= 123; ! ! -- select tuples with some txt element matching '[a-z]' ! select * from t where t.txt *~ '[a-z]'; ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where t.txt[1:3] **~ '^[A-Z]'; ! ! The scheme is quite general, each operator which operates on a base type ! can be iterated over the elements of an array. It seem to work well but ! defining each new operator requires writing a different C function. ! This is tedious, and error-prone since one must take care that the correct ! datatypes are associated with the selected underlying function. ! Can anyone suggest a better and more portable way to do it ? - See also array_iterator.sql for an example on how to use this module. --- 1,32 ---- ! Array iterator functions have been removed as of PostgreSQL 7.4, because ! equivalent functionality is now available built in to the backend. ! For example, previously, using contrib/array, you might have used the ! following construct: + create table t(id int4[], txt text[]); ! -- select tuples with some id element equal to 123 ! select * from t where t.id *= 123; ! ! Now you would do this instead: ! ! -- select tuples with some id element equal to 123 ! select * from t where 123 = any (t.id); ! ! -- or you could also do this ! select * from t where 123 = some (t.id); ! ! Similarly, if using contrib/array, you did the following: ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where t.txt[1:3] **~ '^[A-Z]'; ! ! Now do this instead: ! ! -- select tuples with all txt elements matching '^[A-Z]' ! select * from t where '^[A-Z]' ~ all (t.txt[1:3]); ! ! See the related section in the online documentation for more detail: ! Table of Contents => Functions and Operators => Row and Array Comparisons Index: contrib/array/array_iterator.c =================================================================== RCS file: contrib/array/array_iterator.c diff -N contrib/array/array_iterator.c *** contrib/array/array_iterator.c 26 May 2003 00:11:27 -0000 1.26 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,333 **** - /* - * array_iterator.c -- - * - * This file defines a new class of operators which take an - * array and a scalar value, iterate a scalar operator over the - * elements of the array and the value and compute a result as - * the logical OR or AND of the iteration results. - * - * Copyright (C) 1999, Massimo Dal Zotto <[email protected]> - * ported to postgreSQL 6.3.2,added oid_functions, 18.1.1999, - * Tobias Gabele <[email protected]> - * - * This software is distributed under the GNU General Public License - * either version 2, or (at your option) any later version. - */ - - #include "postgres.h" - - #include <ctype.h> - #include <stdio.h> - #include <sys/types.h> - #include <string.h> - - #include "access/tupmacs.h" - #include "access/xact.h" - #include "fmgr.h" - #include "miscadmin.h" - #include "utils/array.h" - #include "utils/builtins.h" - #include "utils/fmgroids.h" - #include "utils/memutils.h" - #include "utils/lsyscache.h" - - #include "array_iterator.h" - - - static int32 - array_iterator(Oid proc, int and, ArrayType *array, Datum value) - { - Oid elemtype; - int16 typlen; - bool typbyval; - char typalign; - int nitems, - i; - Datum result; - int ndim, - *dim; - char *p; - FmgrInfo finfo; - - /* Sanity checks */ - if (array == (ArrayType *) NULL) - { - /* elog(WARNING, "array_iterator: array is null"); */ - return (0); - } - - /* detoast input if necessary */ - array = DatumGetArrayTypeP(PointerGetDatum(array)); - - ndim = ARR_NDIM(array); - dim = ARR_DIMS(array); - nitems = ArrayGetNItems(ndim, dim); - if (nitems == 0) - return (0); - - /* Lookup element type information */ - elemtype = ARR_ELEMTYPE(array); - get_typlenbyvalalign(elemtype, &typlen, &typbyval, &typalign); - - /* Lookup the function entry point */ - fmgr_info(proc, &finfo); - if (finfo.fn_nargs != 2) - { - elog(ERROR, "array_iterator: proc %u does not take 2 args", proc); - return (0); - } - - /* Scan the array and apply the operator to each element */ - result = BoolGetDatum(false); - p = ARR_DATA_PTR(array); - for (i = 0; i < nitems; i++) - { - Datum itemvalue; - - itemvalue = fetch_att(p, typbyval, typlen); - - p = att_addlength(p, typlen, PointerGetDatum(p)); - p = (char *) att_align(p, typalign); - - result = FunctionCall2(&finfo, itemvalue, value); - - if (DatumGetBool(result)) - { - if (!and) - return (1); - } - else - { - if (and) - return (0); - } - } - - if (and && DatumGetBool(result)) - return (1); - else - return (0); - } - - /* - * Iterator functions for type _text - */ - - int32 - array_texteq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_texteq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTEQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_textregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_textregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 1, /* logical and */ - array, (Datum) value); - } - - /* - * Iterator functions for type _bpchar. Note that the regexp - * operators take the second argument of type text. - */ - - int32 - array_bpchareq(ArrayType *array, void *value) - { - return array_iterator(F_BPCHAREQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_bpchareq(ArrayType *array, void *value) - { - return array_iterator(F_BPCHAREQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_bpcharregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_bpcharregexeq(ArrayType *array, void *value) - { - return array_iterator(F_TEXTREGEXEQ, - 1, /* logical and */ - array, (Datum) value); - } - - /* - * Iterator functions for type _int4 - */ - - int32 - array_int4eq(ArrayType *array, int4 value) - { - return array_iterator(F_INT4EQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4eq(ArrayType *array, int4 value) - { - return array_iterator(F_INT4EQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4ne(ArrayType *array, int4 value) - { - return array_iterator(F_INT4NE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4ne(ArrayType *array, int4 value) - { - return array_iterator(F_INT4NE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4gt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GT, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4gt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GT, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4ge(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4ge(ArrayType *array, int4 value) - { - return array_iterator(F_INT4GE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4lt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LT, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4lt(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LT, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_int4le(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LE, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_int4le(ArrayType *array, int4 value) - { - return array_iterator(F_INT4LE, - 1, /* logical and */ - array, (Datum) value); - } - - /* new tobias gabele 1999 */ - - int32 - array_oideq(ArrayType *array, Oid value) - { - return array_iterator(F_OIDEQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_oidne(ArrayType *array, Oid value) - { - return array_iterator(F_OIDNE, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_ineteq(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_EQ, - 0, /* logical or */ - array, (Datum) value); - } - - int32 - array_all_ineteq(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_EQ, - 1, /* logical and */ - array, (Datum) value); - } - - int32 - array_inetne(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_NE, - 0, /* logical and */ - array, (Datum) value); - } - - int32 - array_all_inetne(ArrayType *array, void *value) - { - return array_iterator(F_NETWORK_NE, - 1, /* logical and */ - array, (Datum) value); - } --- 0 ---- Index: contrib/array/array_iterator.h =================================================================== RCS file: contrib/array/array_iterator.h diff -N contrib/array/array_iterator.h *** contrib/array/array_iterator.h 26 May 2003 00:11:27 -0000 1.10 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,38 **** - #ifndef ARRAY_ITERATOR_H - #define ARRAY_ITERATOR_H - - static int32 array_iterator(Oid proc, int and, - ArrayType *array, Datum value); - - int32 array_texteq(ArrayType *array, void *value); - int32 array_all_texteq(ArrayType *array, void *value); - int32 array_textregexeq(ArrayType *array, void *value); - int32 array_all_textregexeq(ArrayType *array, void *value); - - int32 array_bpchareq(ArrayType *array, void *value); - int32 array_all_bpchareq(ArrayType *array, void *value); - int32 array_bpcharregexeq(ArrayType *array, void *value); - int32 array_all_bpcharregexeq(ArrayType *array, void *value); - - int32 array_int4eq(ArrayType *array, int4 value); - int32 array_all_int4eq(ArrayType *array, int4 value); - int32 array_int4ne(ArrayType *array, int4 value); - int32 array_all_int4ne(ArrayType *array, int4 value); - int32 array_int4gt(ArrayType *array, int4 value); - int32 array_all_int4gt(ArrayType *array, int4 value); - int32 array_int4ge(ArrayType *array, int4 value); - int32 array_all_int4ge(ArrayType *array, int4 value); - int32 array_int4lt(ArrayType *array, int4 value); - int32 array_all_int4lt(ArrayType *array, int4 value); - int32 array_int4le(ArrayType *array, int4 value); - int32 array_all_int4le(ArrayType *array, int4 value); - - int32 array_oideq(ArrayType *array, Oid value); - int32 array_all_oidne(ArrayType *array, Oid value); - - int32 array_ineteq(ArrayType *array, void *value); - int32 array_all_ineteq(ArrayType *array, void *value); - int32 array_inetne(ArrayType *array, void *value); - int32 array_all_inetne(ArrayType *array, void *value); - - #endif --- 0 ---- Index: contrib/array/array_iterator.sql.in =================================================================== RCS file: contrib/array/array_iterator.sql.in diff -N contrib/array/array_iterator.sql.in *** contrib/array/array_iterator.sql.in 26 May 2003 00:11:27 -0000 1.10 --- /dev/null 1 Jan 1970 00:00:00 -0000 *************** *** 1,329 **** - -- SQL code to define the new array iterator functions and operators - - -- define the array operators *=, **=, *~ and **~ for type _text - -- - - -- Adjust this setting to control where the objects get created. - SET search_path = public; - - CREATE OR REPLACE FUNCTION array_texteq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_texteq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_textregexeq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_textregexeq(_text, text) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_text,text); - CREATE OPERATOR *= ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_texteq - ); - - DROP OPERATOR **=(_text,text); - CREATE OPERATOR **= ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_all_texteq - ); - - DROP OPERATOR *~(_text,text); - CREATE OPERATOR *~ ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_textregexeq - ); - - DROP OPERATOR **~(_text,text); - CREATE OPERATOR **~ ( - LEFTARG=_text, - RIGHTARG=text, - PROCEDURE=array_all_textregexeq - ); - - - -- define the array operators *=, **=, *~ and **~ for type _bpchar - -- - CREATE OR REPLACE FUNCTION array_bpchareq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_bpchareq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_bpcharregexeq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_bpcharregexeq(_bpchar, bpchar) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_bpchar,bpchar); - CREATE OPERATOR *= ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_bpchareq - ); - - DROP OPERATOR **=(_bpchar,bpchar); - CREATE OPERATOR **= ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_all_bpchareq - ); - - DROP OPERATOR *~(_bpchar,bpchar); - CREATE OPERATOR *~ ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_bpcharregexeq - ); - - DROP OPERATOR **~(_bpchar,bpchar); - CREATE OPERATOR **~ ( - LEFTARG=_bpchar, - RIGHTARG=bpchar, - PROCEDURE=array_all_bpcharregexeq - ); - - - -- define the array operators *=, **=, *> and **> for type _int4 - -- - CREATE OR REPLACE FUNCTION array_int4eq(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4eq(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4ne(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4ne(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4gt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4gt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4ge(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4ge(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4lt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4lt(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_int4le(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_int4le(_int4, int4) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_int4,int4); - CREATE OPERATOR *= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4eq - ); - - DROP OPERATOR **=(_int4,int4); - CREATE OPERATOR **= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4eq - ); - - DROP OPERATOR *<>(_int4,int4); - CREATE OPERATOR *<> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4ne - ); - - DROP OPERATOR **<>(_int4,int4); - CREATE OPERATOR **<> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4ne - ); - - DROP OPERATOR *>(_int4,int4); - CREATE OPERATOR *> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4gt - ); - - DROP OPERATOR **>(_int4,int4); - CREATE OPERATOR **> ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4gt - ); - - DROP OPERATOR *>=(_int4,int4); - CREATE OPERATOR *>= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4ge - ); - - DROP OPERATOR **>=(_int4,int4); - CREATE OPERATOR **>= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4ge - ); - - DROP OPERATOR *<(_int4,int4); - CREATE OPERATOR *< ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4lt - ); - - DROP OPERATOR **<(_int4,int4); - CREATE OPERATOR **< ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4lt - ); - - DROP OPERATOR *<=(_int4,int4); - CREATE OPERATOR *<= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_int4le - ); - - DROP OPERATOR **<=(_int4,int4); - CREATE OPERATOR **<= ( - LEFTARG=_int4, - RIGHTARG=int4, - PROCEDURE=array_all_int4le - ); - - -- define the array operators *=, **<> for type _oid (added tobias 1. 1999) - -- - CREATE OR REPLACE FUNCTION array_oideq(_oid, oid) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_oidne(_oid, oid) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_oid,oid); - CREATE OPERATOR *= ( - LEFTARG=_oid, - RIGHTARG=oid, - PROCEDURE=array_oideq - ); - - DROP OPERATOR **<>(_oid,oid); - CREATE OPERATOR **<> ( - LEFTARG=_oid, - RIGHTARG=oid, - PROCEDURE=array_all_oidne - ); - - -- define the array operators *=, **=, *<>, **<> for type _inet - - CREATE OR REPLACE FUNCTION array_ineteq(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_ineteq(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_inetne(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - CREATE OR REPLACE FUNCTION array_all_inetne(_inet, inet) - RETURNS bool - AS 'MODULE_PATHNAME' - LANGUAGE 'C' IMMUTABLE STRICT; - - DROP OPERATOR *=(_inet,inet); - CREATE OPERATOR *= ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_ineteq - ); - - DROP OPERATOR **=(_inet,inet); - CREATE OPERATOR **= ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_all_ineteq - ); - - DROP OPERATOR *<>(_inet,inet); - CREATE OPERATOR *<> ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_inetne - ); - - DROP OPERATOR **<>(_inet,inet); - CREATE OPERATOR **<> ( - LEFTARG=_inet, - RIGHTARG=inet, - PROCEDURE=array_all_inetne - ); --- 0 ---- Index: contrib/Makefile =================================================================== RCS file: /opt/src/cvs/pgsql-server/contrib/Makefile,v retrieving revision 1.45 diff -c -r1.45 Makefile *** contrib/Makefile 24 Jul 2003 16:54:58 -0000 1.45 --- contrib/Makefile 31 Aug 2003 04:47:01 -0000 *************** *** 5,11 **** include $(top_builddir)/src/Makefile.global WANTED_DIRS = \ - array \ btree_gist \ chkpass \ cube \ --- 5,10 ---- *************** *** 44,49 **** --- 43,49 ---- vacuumlo # Missing: + # array \ (removed all but the README) # adddepend \ (does not have a makefile) # ipc_check \ (does not have a makefile) # mSQL-interface \ (requires msql installed) ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: [HACKERS] New array functions @ 2003-09-10 04:14 Bruce Momjian <[email protected]> parent: Joe Conway <[email protected]> 1 sibling, 0 replies; 280+ messages in thread From: Bruce Momjian @ 2003-09-10 04:14 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Tom Lane <[email protected]>; Greg Stark <[email protected]>; Patches (PostgreSQL) <[email protected]> Your patch has been added to the PostgreSQL unapplied patches list at: http://momjian.postgresql.org/cgi-bin/pgpatches I will try to apply it within the next 48 hours. --------------------------------------------------------------------------- Joe Conway wrote: > Joe Conway wrote: > > Tom Lane wrote: > >> Did we discuss this already? I'd forgotten. > >> > > I can't find it in the archives for some reason, but here was the exchange: > > > > Tom Lane wrote: > > > Bruce Momjian <[email protected]> writes: > > > > > >>Joe Conway wrote: > > >> > > >>>I do agree that it makes contrib/array unnecessary. I was going to > > >>>suggest we remove that if this was committed. > > > > > >>Good idea. > > > > > > We could do that, but it might be more friendly to just mark it as > > > deprecated for one release cycle before zapping it. That'd give > > > people who use it some time to convert over. > > > > So I guess since it was actually you who objected, you have the right to > > change your mind ;-) > > > > Here is a patch that removes contrib/array, leaving only the README with > some examples of the new syntax and a reference to the documentation. > > I'll try to take a look at contrib/intarray and contrib/intagg before > the weekend is through, and at least post a recommendation. > > Joe > > Index: contrib/array/Makefile > =================================================================== > RCS file: contrib/array/Makefile > diff -N contrib/array/Makefile > *** contrib/array/Makefile 6 Sep 2001 10:49:29 -0000 1.16 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,11 **** > - # $Header: /opt/src/cvs/pgsql-server/contrib/array/Makefile,v 1.16 2001/09/06 10:49:29 petere Exp $ > - > - subdir = contrib/array > - top_builddir = ../.. > - include $(top_builddir)/src/Makefile.global > - > - MODULES = array_iterator > - DATA_built = array_iterator.sql > - DOCS = README.array_iterator > - > - include $(top_srcdir)/contrib/contrib-global.mk > --- 0 ---- > Index: contrib/array/README.array_iterator > =================================================================== > RCS file: /opt/src/cvs/pgsql-server/contrib/array/README.array_iterator,v > retrieving revision 1.2 > diff -c -r1.2 README.array_iterator > *** contrib/array/README.array_iterator 26 Aug 2002 17:53:57 -0000 1.2 > --- contrib/array/README.array_iterator 31 Aug 2003 04:37:04 -0000 > *************** > *** 1,49 **** > ! Array iterator functions, by Massimo Dal Zotto <[email protected]> > ! Copyright (C) 1999, Massimo Dal Zotto <[email protected]> > > ! This software is distributed under the GNU General Public License > ! either version 2, or (at your option) any later version. > > > ! This loadable module defines a new class of functions which take > ! an array and a scalar value, iterate a scalar operator over the > ! elements of the array and the value, and compute a result as > ! the logical OR or AND of the iteration results. > ! For example array_int4eq returns true if some of the elements > ! of an array of int4 is equal to the given value: > ! > ! array_int4eq({1,2,3}, 1) --> true > ! array_int4eq({1,2,3}, 4) --> false > ! > ! If we have defined T array types and O scalar operators we can > ! define T x O x 2 array functions, each of them has a name like > ! "array_[all_]<basetype><operation>" and takes an array of type T > ! iterating the operator O over all the elements. Note however > ! that some of the possible combination are invalid, for example > ! the array_int4_like because there is no like operator for int4. > ! > ! We can then define new operators based on these functions and use > ! them to write queries with qualification clauses based on the > ! values of some of the elements of an array. > ! For example to select rows having some or all element of an array > ! attribute equal to a given value or matching a regular expression: > ! > ! create table t(id int4[], txt text[]); > ! > ! -- select tuples with some id element equal to 123 > ! select * from t where t.id *= 123; > ! > ! -- select tuples with some txt element matching '[a-z]' > ! select * from t where t.txt *~ '[a-z]'; > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where t.txt[1:3] **~ '^[A-Z]'; > ! > ! The scheme is quite general, each operator which operates on a base type > ! can be iterated over the elements of an array. It seem to work well but > ! defining each new operator requires writing a different C function. > ! This is tedious, and error-prone since one must take care that the correct > ! datatypes are associated with the selected underlying function. > ! Can anyone suggest a better and more portable way to do it ? > > - See also array_iterator.sql for an example on how to use this module. > --- 1,32 ---- > ! Array iterator functions have been removed as of PostgreSQL 7.4, because > ! equivalent functionality is now available built in to the backend. > > ! For example, previously, using contrib/array, you might have used the > ! following construct: > > + create table t(id int4[], txt text[]); > > ! -- select tuples with some id element equal to 123 > ! select * from t where t.id *= 123; > ! > ! Now you would do this instead: > ! > ! -- select tuples with some id element equal to 123 > ! select * from t where 123 = any (t.id); > ! > ! -- or you could also do this > ! select * from t where 123 = some (t.id); > ! > ! Similarly, if using contrib/array, you did the following: > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where t.txt[1:3] **~ '^[A-Z]'; > ! > ! Now do this instead: > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where '^[A-Z]' ~ all (t.txt[1:3]); > ! > ! See the related section in the online documentation for more detail: > ! Table of Contents => Functions and Operators => Row and Array Comparisons > > Index: contrib/array/array_iterator.c > =================================================================== > RCS file: contrib/array/array_iterator.c > diff -N contrib/array/array_iterator.c > *** contrib/array/array_iterator.c 26 May 2003 00:11:27 -0000 1.26 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,333 **** > - /* > - * array_iterator.c -- > - * > - * This file defines a new class of operators which take an > - * array and a scalar value, iterate a scalar operator over the > - * elements of the array and the value and compute a result as > - * the logical OR or AND of the iteration results. > - * > - * Copyright (C) 1999, Massimo Dal Zotto <[email protected]> > - * ported to postgreSQL 6.3.2,added oid_functions, 18.1.1999, > - * Tobias Gabele <[email protected]> > - * > - * This software is distributed under the GNU General Public License > - * either version 2, or (at your option) any later version. > - */ > - > - #include "postgres.h" > - > - #include <ctype.h> > - #include <stdio.h> > - #include <sys/types.h> > - #include <string.h> > - > - #include "access/tupmacs.h" > - #include "access/xact.h" > - #include "fmgr.h" > - #include "miscadmin.h" > - #include "utils/array.h" > - #include "utils/builtins.h" > - #include "utils/fmgroids.h" > - #include "utils/memutils.h" > - #include "utils/lsyscache.h" > - > - #include "array_iterator.h" > - > - > - static int32 > - array_iterator(Oid proc, int and, ArrayType *array, Datum value) > - { > - Oid elemtype; > - int16 typlen; > - bool typbyval; > - char typalign; > - int nitems, > - i; > - Datum result; > - int ndim, > - *dim; > - char *p; > - FmgrInfo finfo; > - > - /* Sanity checks */ > - if (array == (ArrayType *) NULL) > - { > - /* elog(WARNING, "array_iterator: array is null"); */ > - return (0); > - } > - > - /* detoast input if necessary */ > - array = DatumGetArrayTypeP(PointerGetDatum(array)); > - > - ndim = ARR_NDIM(array); > - dim = ARR_DIMS(array); > - nitems = ArrayGetNItems(ndim, dim); > - if (nitems == 0) > - return (0); > - > - /* Lookup element type information */ > - elemtype = ARR_ELEMTYPE(array); > - get_typlenbyvalalign(elemtype, &typlen, &typbyval, &typalign); > - > - /* Lookup the function entry point */ > - fmgr_info(proc, &finfo); > - if (finfo.fn_nargs != 2) > - { > - elog(ERROR, "array_iterator: proc %u does not take 2 args", proc); > - return (0); > - } > - > - /* Scan the array and apply the operator to each element */ > - result = BoolGetDatum(false); > - p = ARR_DATA_PTR(array); > - for (i = 0; i < nitems; i++) > - { > - Datum itemvalue; > - > - itemvalue = fetch_att(p, typbyval, typlen); > - > - p = att_addlength(p, typlen, PointerGetDatum(p)); > - p = (char *) att_align(p, typalign); > - > - result = FunctionCall2(&finfo, itemvalue, value); > - > - if (DatumGetBool(result)) > - { > - if (!and) > - return (1); > - } > - else > - { > - if (and) > - return (0); > - } > - } > - > - if (and && DatumGetBool(result)) > - return (1); > - else > - return (0); > - } > - > - /* > - * Iterator functions for type _text > - */ > - > - int32 > - array_texteq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_texteq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_textregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_textregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* > - * Iterator functions for type _bpchar. Note that the regexp > - * operators take the second argument of type text. > - */ > - > - int32 > - array_bpchareq(ArrayType *array, void *value) > - { > - return array_iterator(F_BPCHAREQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_bpchareq(ArrayType *array, void *value) > - { > - return array_iterator(F_BPCHAREQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_bpcharregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_bpcharregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* > - * Iterator functions for type _int4 > - */ > - > - int32 > - array_int4eq(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4EQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4eq(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4EQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4ne(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4NE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4ne(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4NE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4gt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GT, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4gt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GT, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4ge(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4ge(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4lt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LT, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4lt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LT, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4le(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4le(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* new tobias gabele 1999 */ > - > - int32 > - array_oideq(ArrayType *array, Oid value) > - { > - return array_iterator(F_OIDEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_oidne(ArrayType *array, Oid value) > - { > - return array_iterator(F_OIDNE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_ineteq(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_EQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_ineteq(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_EQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_inetne(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_NE, > - 0, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_all_inetne(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_NE, > - 1, /* logical and */ > - array, (Datum) value); > - } > --- 0 ---- > Index: contrib/array/array_iterator.h > =================================================================== > RCS file: contrib/array/array_iterator.h > diff -N contrib/array/array_iterator.h > *** contrib/array/array_iterator.h 26 May 2003 00:11:27 -0000 1.10 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,38 **** > - #ifndef ARRAY_ITERATOR_H > - #define ARRAY_ITERATOR_H > - > - static int32 array_iterator(Oid proc, int and, > - ArrayType *array, Datum value); > - > - int32 array_texteq(ArrayType *array, void *value); > - int32 array_all_texteq(ArrayType *array, void *value); > - int32 array_textregexeq(ArrayType *array, void *value); > - int32 array_all_textregexeq(ArrayType *array, void *value); > - > - int32 array_bpchareq(ArrayType *array, void *value); > - int32 array_all_bpchareq(ArrayType *array, void *value); > - int32 array_bpcharregexeq(ArrayType *array, void *value); > - int32 array_all_bpcharregexeq(ArrayType *array, void *value); > - > - int32 array_int4eq(ArrayType *array, int4 value); > - int32 array_all_int4eq(ArrayType *array, int4 value); > - int32 array_int4ne(ArrayType *array, int4 value); > - int32 array_all_int4ne(ArrayType *array, int4 value); > - int32 array_int4gt(ArrayType *array, int4 value); > - int32 array_all_int4gt(ArrayType *array, int4 value); > - int32 array_int4ge(ArrayType *array, int4 value); > - int32 array_all_int4ge(ArrayType *array, int4 value); > - int32 array_int4lt(ArrayType *array, int4 value); > - int32 array_all_int4lt(ArrayType *array, int4 value); > - int32 array_int4le(ArrayType *array, int4 value); > - int32 array_all_int4le(ArrayType *array, int4 value); > - > - int32 array_oideq(ArrayType *array, Oid value); > - int32 array_all_oidne(ArrayType *array, Oid value); > - > - int32 array_ineteq(ArrayType *array, void *value); > - int32 array_all_ineteq(ArrayType *array, void *value); > - int32 array_inetne(ArrayType *array, void *value); > - int32 array_all_inetne(ArrayType *array, void *value); > - > - #endif > --- 0 ---- > Index: contrib/array/array_iterator.sql.in > =================================================================== > RCS file: contrib/array/array_iterator.sql.in > diff -N contrib/array/array_iterator.sql.in > *** contrib/array/array_iterator.sql.in 26 May 2003 00:11:27 -0000 1.10 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,329 **** > - -- SQL code to define the new array iterator functions and operators > - > - -- define the array operators *=, **=, *~ and **~ for type _text > - -- > - > - -- Adjust this setting to control where the objects get created. > - SET search_path = public; > - > - CREATE OR REPLACE FUNCTION array_texteq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_texteq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_textregexeq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_textregexeq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_text,text); > - CREATE OPERATOR *= ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_texteq > - ); > - > - DROP OPERATOR **=(_text,text); > - CREATE OPERATOR **= ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_all_texteq > - ); > - > - DROP OPERATOR *~(_text,text); > - CREATE OPERATOR *~ ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_textregexeq > - ); > - > - DROP OPERATOR **~(_text,text); > - CREATE OPERATOR **~ ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_all_textregexeq > - ); > - > - > - -- define the array operators *=, **=, *~ and **~ for type _bpchar > - -- > - CREATE OR REPLACE FUNCTION array_bpchareq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_bpchareq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_bpcharregexeq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_bpcharregexeq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_bpchar,bpchar); > - CREATE OPERATOR *= ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_bpchareq > - ); > - > - DROP OPERATOR **=(_bpchar,bpchar); > - CREATE OPERATOR **= ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_all_bpchareq > - ); > - > - DROP OPERATOR *~(_bpchar,bpchar); > - CREATE OPERATOR *~ ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_bpcharregexeq > - ); > - > - DROP OPERATOR **~(_bpchar,bpchar); > - CREATE OPERATOR **~ ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_all_bpcharregexeq > - ); > - > - > - -- define the array operators *=, **=, *> and **> for type _int4 > - -- > - CREATE OR REPLACE FUNCTION array_int4eq(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4eq(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4ne(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4ne(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4gt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4gt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4ge(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4ge(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4lt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4lt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4le(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4le(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_int4,int4); > - CREATE OPERATOR *= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4eq > - ); > - > - DROP OPERATOR **=(_int4,int4); > - CREATE OPERATOR **= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4eq > - ); > - > - DROP OPERATOR *<>(_int4,int4); > - CREATE OPERATOR *<> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4ne > - ); > - > - DROP OPERATOR **<>(_int4,int4); > - CREATE OPERATOR **<> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4ne > - ); > - > - DROP OPERATOR *>(_int4,int4); > - CREATE OPERATOR *> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4gt > - ); > - > - DROP OPERATOR **>(_int4,int4); > - CREATE OPERATOR **> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4gt > - ); > - > - DROP OPERATOR *>=(_int4,int4); > - CREATE OPERATOR *>= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4ge > - ); > - > - DROP OPERATOR **>=(_int4,int4); > - CREATE OPERATOR **>= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4ge > - ); > - > - DROP OPERATOR *<(_int4,int4); > - CREATE OPERATOR *< ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4lt > - ); > - > - DROP OPERATOR **<(_int4,int4); > - CREATE OPERATOR **< ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4lt > - ); > - > - DROP OPERATOR *<=(_int4,int4); > - CREATE OPERATOR *<= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4le > - ); > - > - DROP OPERATOR **<=(_int4,int4); > - CREATE OPERATOR **<= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4le > - ); > - > - -- define the array operators *=, **<> for type _oid (added tobias 1. 1999) > - -- > - CREATE OR REPLACE FUNCTION array_oideq(_oid, oid) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_oidne(_oid, oid) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_oid,oid); > - CREATE OPERATOR *= ( > - LEFTARG=_oid, > - RIGHTARG=oid, > - PROCEDURE=array_oideq > - ); > - > - DROP OPERATOR **<>(_oid,oid); > - CREATE OPERATOR **<> ( > - LEFTARG=_oid, > - RIGHTARG=oid, > - PROCEDURE=array_all_oidne > - ); > - > - -- define the array operators *=, **=, *<>, **<> for type _inet > - > - CREATE OR REPLACE FUNCTION array_ineteq(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_ineteq(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_inetne(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_inetne(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_inet,inet); > - CREATE OPERATOR *= ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_ineteq > - ); > - > - DROP OPERATOR **=(_inet,inet); > - CREATE OPERATOR **= ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_all_ineteq > - ); > - > - DROP OPERATOR *<>(_inet,inet); > - CREATE OPERATOR *<> ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_inetne > - ); > - > - DROP OPERATOR **<>(_inet,inet); > - CREATE OPERATOR **<> ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_all_inetne > - ); > --- 0 ---- > Index: contrib/Makefile > =================================================================== > RCS file: /opt/src/cvs/pgsql-server/contrib/Makefile,v > retrieving revision 1.45 > diff -c -r1.45 Makefile > *** contrib/Makefile 24 Jul 2003 16:54:58 -0000 1.45 > --- contrib/Makefile 31 Aug 2003 04:47:01 -0000 > *************** > *** 5,11 **** > include $(top_builddir)/src/Makefile.global > > WANTED_DIRS = \ > - array \ > btree_gist \ > chkpass \ > cube \ > --- 5,10 ---- > *************** > *** 44,49 **** > --- 43,49 ---- > vacuumlo > > # Missing: > + # array \ (removed all but the README) > # adddepend \ (does not have a makefile) > # ipc_check \ (does not have a makefile) > # mSQL-interface \ (requires msql installed) > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org -- Bruce Momjian | http://candle.pha.pa.us [email protected] | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073 ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: [HACKERS] New array functions @ 2003-09-11 17:15 Bruce Momjian <[email protected]> parent: Joe Conway <[email protected]> 1 sibling, 1 reply; 280+ messages in thread From: Bruce Momjian @ 2003-09-11 17:15 UTC (permalink / raw) To: Joe Conway <[email protected]>; +Cc: Tom Lane <[email protected]>; Greg Stark <[email protected]>; Patches (PostgreSQL) <[email protected]> Patch applied. Thanks. --------------------------------------------------------------------------- Joe Conway wrote: > Joe Conway wrote: > > Tom Lane wrote: > >> Did we discuss this already? I'd forgotten. > >> > > I can't find it in the archives for some reason, but here was the exchange: > > > > Tom Lane wrote: > > > Bruce Momjian <[email protected]> writes: > > > > > >>Joe Conway wrote: > > >> > > >>>I do agree that it makes contrib/array unnecessary. I was going to > > >>>suggest we remove that if this was committed. > > > > > >>Good idea. > > > > > > We could do that, but it might be more friendly to just mark it as > > > deprecated for one release cycle before zapping it. That'd give > > > people who use it some time to convert over. > > > > So I guess since it was actually you who objected, you have the right to > > change your mind ;-) > > > > Here is a patch that removes contrib/array, leaving only the README with > some examples of the new syntax and a reference to the documentation. > > I'll try to take a look at contrib/intarray and contrib/intagg before > the weekend is through, and at least post a recommendation. > > Joe > > Index: contrib/array/Makefile > =================================================================== > RCS file: contrib/array/Makefile > diff -N contrib/array/Makefile > *** contrib/array/Makefile 6 Sep 2001 10:49:29 -0000 1.16 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,11 **** > - # $Header: /opt/src/cvs/pgsql-server/contrib/array/Makefile,v 1.16 2001/09/06 10:49:29 petere Exp $ > - > - subdir = contrib/array > - top_builddir = ../.. > - include $(top_builddir)/src/Makefile.global > - > - MODULES = array_iterator > - DATA_built = array_iterator.sql > - DOCS = README.array_iterator > - > - include $(top_srcdir)/contrib/contrib-global.mk > --- 0 ---- > Index: contrib/array/README.array_iterator > =================================================================== > RCS file: /opt/src/cvs/pgsql-server/contrib/array/README.array_iterator,v > retrieving revision 1.2 > diff -c -r1.2 README.array_iterator > *** contrib/array/README.array_iterator 26 Aug 2002 17:53:57 -0000 1.2 > --- contrib/array/README.array_iterator 31 Aug 2003 04:37:04 -0000 > *************** > *** 1,49 **** > ! Array iterator functions, by Massimo Dal Zotto <[email protected]> > ! Copyright (C) 1999, Massimo Dal Zotto <[email protected]> > > ! This software is distributed under the GNU General Public License > ! either version 2, or (at your option) any later version. > > > ! This loadable module defines a new class of functions which take > ! an array and a scalar value, iterate a scalar operator over the > ! elements of the array and the value, and compute a result as > ! the logical OR or AND of the iteration results. > ! For example array_int4eq returns true if some of the elements > ! of an array of int4 is equal to the given value: > ! > ! array_int4eq({1,2,3}, 1) --> true > ! array_int4eq({1,2,3}, 4) --> false > ! > ! If we have defined T array types and O scalar operators we can > ! define T x O x 2 array functions, each of them has a name like > ! "array_[all_]<basetype><operation>" and takes an array of type T > ! iterating the operator O over all the elements. Note however > ! that some of the possible combination are invalid, for example > ! the array_int4_like because there is no like operator for int4. > ! > ! We can then define new operators based on these functions and use > ! them to write queries with qualification clauses based on the > ! values of some of the elements of an array. > ! For example to select rows having some or all element of an array > ! attribute equal to a given value or matching a regular expression: > ! > ! create table t(id int4[], txt text[]); > ! > ! -- select tuples with some id element equal to 123 > ! select * from t where t.id *= 123; > ! > ! -- select tuples with some txt element matching '[a-z]' > ! select * from t where t.txt *~ '[a-z]'; > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where t.txt[1:3] **~ '^[A-Z]'; > ! > ! The scheme is quite general, each operator which operates on a base type > ! can be iterated over the elements of an array. It seem to work well but > ! defining each new operator requires writing a different C function. > ! This is tedious, and error-prone since one must take care that the correct > ! datatypes are associated with the selected underlying function. > ! Can anyone suggest a better and more portable way to do it ? > > - See also array_iterator.sql for an example on how to use this module. > --- 1,32 ---- > ! Array iterator functions have been removed as of PostgreSQL 7.4, because > ! equivalent functionality is now available built in to the backend. > > ! For example, previously, using contrib/array, you might have used the > ! following construct: > > + create table t(id int4[], txt text[]); > > ! -- select tuples with some id element equal to 123 > ! select * from t where t.id *= 123; > ! > ! Now you would do this instead: > ! > ! -- select tuples with some id element equal to 123 > ! select * from t where 123 = any (t.id); > ! > ! -- or you could also do this > ! select * from t where 123 = some (t.id); > ! > ! Similarly, if using contrib/array, you did the following: > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where t.txt[1:3] **~ '^[A-Z]'; > ! > ! Now do this instead: > ! > ! -- select tuples with all txt elements matching '^[A-Z]' > ! select * from t where '^[A-Z]' ~ all (t.txt[1:3]); > ! > ! See the related section in the online documentation for more detail: > ! Table of Contents => Functions and Operators => Row and Array Comparisons > > Index: contrib/array/array_iterator.c > =================================================================== > RCS file: contrib/array/array_iterator.c > diff -N contrib/array/array_iterator.c > *** contrib/array/array_iterator.c 26 May 2003 00:11:27 -0000 1.26 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,333 **** > - /* > - * array_iterator.c -- > - * > - * This file defines a new class of operators which take an > - * array and a scalar value, iterate a scalar operator over the > - * elements of the array and the value and compute a result as > - * the logical OR or AND of the iteration results. > - * > - * Copyright (C) 1999, Massimo Dal Zotto <[email protected]> > - * ported to postgreSQL 6.3.2,added oid_functions, 18.1.1999, > - * Tobias Gabele <[email protected]> > - * > - * This software is distributed under the GNU General Public License > - * either version 2, or (at your option) any later version. > - */ > - > - #include "postgres.h" > - > - #include <ctype.h> > - #include <stdio.h> > - #include <sys/types.h> > - #include <string.h> > - > - #include "access/tupmacs.h" > - #include "access/xact.h" > - #include "fmgr.h" > - #include "miscadmin.h" > - #include "utils/array.h" > - #include "utils/builtins.h" > - #include "utils/fmgroids.h" > - #include "utils/memutils.h" > - #include "utils/lsyscache.h" > - > - #include "array_iterator.h" > - > - > - static int32 > - array_iterator(Oid proc, int and, ArrayType *array, Datum value) > - { > - Oid elemtype; > - int16 typlen; > - bool typbyval; > - char typalign; > - int nitems, > - i; > - Datum result; > - int ndim, > - *dim; > - char *p; > - FmgrInfo finfo; > - > - /* Sanity checks */ > - if (array == (ArrayType *) NULL) > - { > - /* elog(WARNING, "array_iterator: array is null"); */ > - return (0); > - } > - > - /* detoast input if necessary */ > - array = DatumGetArrayTypeP(PointerGetDatum(array)); > - > - ndim = ARR_NDIM(array); > - dim = ARR_DIMS(array); > - nitems = ArrayGetNItems(ndim, dim); > - if (nitems == 0) > - return (0); > - > - /* Lookup element type information */ > - elemtype = ARR_ELEMTYPE(array); > - get_typlenbyvalalign(elemtype, &typlen, &typbyval, &typalign); > - > - /* Lookup the function entry point */ > - fmgr_info(proc, &finfo); > - if (finfo.fn_nargs != 2) > - { > - elog(ERROR, "array_iterator: proc %u does not take 2 args", proc); > - return (0); > - } > - > - /* Scan the array and apply the operator to each element */ > - result = BoolGetDatum(false); > - p = ARR_DATA_PTR(array); > - for (i = 0; i < nitems; i++) > - { > - Datum itemvalue; > - > - itemvalue = fetch_att(p, typbyval, typlen); > - > - p = att_addlength(p, typlen, PointerGetDatum(p)); > - p = (char *) att_align(p, typalign); > - > - result = FunctionCall2(&finfo, itemvalue, value); > - > - if (DatumGetBool(result)) > - { > - if (!and) > - return (1); > - } > - else > - { > - if (and) > - return (0); > - } > - } > - > - if (and && DatumGetBool(result)) > - return (1); > - else > - return (0); > - } > - > - /* > - * Iterator functions for type _text > - */ > - > - int32 > - array_texteq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_texteq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_textregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_textregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* > - * Iterator functions for type _bpchar. Note that the regexp > - * operators take the second argument of type text. > - */ > - > - int32 > - array_bpchareq(ArrayType *array, void *value) > - { > - return array_iterator(F_BPCHAREQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_bpchareq(ArrayType *array, void *value) > - { > - return array_iterator(F_BPCHAREQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_bpcharregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_bpcharregexeq(ArrayType *array, void *value) > - { > - return array_iterator(F_TEXTREGEXEQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* > - * Iterator functions for type _int4 > - */ > - > - int32 > - array_int4eq(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4EQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4eq(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4EQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4ne(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4NE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4ne(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4NE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4gt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GT, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4gt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GT, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4ge(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4ge(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4GE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4lt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LT, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4lt(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LT, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_int4le(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LE, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_int4le(ArrayType *array, int4 value) > - { > - return array_iterator(F_INT4LE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - /* new tobias gabele 1999 */ > - > - int32 > - array_oideq(ArrayType *array, Oid value) > - { > - return array_iterator(F_OIDEQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_oidne(ArrayType *array, Oid value) > - { > - return array_iterator(F_OIDNE, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_ineteq(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_EQ, > - 0, /* logical or */ > - array, (Datum) value); > - } > - > - int32 > - array_all_ineteq(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_EQ, > - 1, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_inetne(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_NE, > - 0, /* logical and */ > - array, (Datum) value); > - } > - > - int32 > - array_all_inetne(ArrayType *array, void *value) > - { > - return array_iterator(F_NETWORK_NE, > - 1, /* logical and */ > - array, (Datum) value); > - } > --- 0 ---- > Index: contrib/array/array_iterator.h > =================================================================== > RCS file: contrib/array/array_iterator.h > diff -N contrib/array/array_iterator.h > *** contrib/array/array_iterator.h 26 May 2003 00:11:27 -0000 1.10 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,38 **** > - #ifndef ARRAY_ITERATOR_H > - #define ARRAY_ITERATOR_H > - > - static int32 array_iterator(Oid proc, int and, > - ArrayType *array, Datum value); > - > - int32 array_texteq(ArrayType *array, void *value); > - int32 array_all_texteq(ArrayType *array, void *value); > - int32 array_textregexeq(ArrayType *array, void *value); > - int32 array_all_textregexeq(ArrayType *array, void *value); > - > - int32 array_bpchareq(ArrayType *array, void *value); > - int32 array_all_bpchareq(ArrayType *array, void *value); > - int32 array_bpcharregexeq(ArrayType *array, void *value); > - int32 array_all_bpcharregexeq(ArrayType *array, void *value); > - > - int32 array_int4eq(ArrayType *array, int4 value); > - int32 array_all_int4eq(ArrayType *array, int4 value); > - int32 array_int4ne(ArrayType *array, int4 value); > - int32 array_all_int4ne(ArrayType *array, int4 value); > - int32 array_int4gt(ArrayType *array, int4 value); > - int32 array_all_int4gt(ArrayType *array, int4 value); > - int32 array_int4ge(ArrayType *array, int4 value); > - int32 array_all_int4ge(ArrayType *array, int4 value); > - int32 array_int4lt(ArrayType *array, int4 value); > - int32 array_all_int4lt(ArrayType *array, int4 value); > - int32 array_int4le(ArrayType *array, int4 value); > - int32 array_all_int4le(ArrayType *array, int4 value); > - > - int32 array_oideq(ArrayType *array, Oid value); > - int32 array_all_oidne(ArrayType *array, Oid value); > - > - int32 array_ineteq(ArrayType *array, void *value); > - int32 array_all_ineteq(ArrayType *array, void *value); > - int32 array_inetne(ArrayType *array, void *value); > - int32 array_all_inetne(ArrayType *array, void *value); > - > - #endif > --- 0 ---- > Index: contrib/array/array_iterator.sql.in > =================================================================== > RCS file: contrib/array/array_iterator.sql.in > diff -N contrib/array/array_iterator.sql.in > *** contrib/array/array_iterator.sql.in 26 May 2003 00:11:27 -0000 1.10 > --- /dev/null 1 Jan 1970 00:00:00 -0000 > *************** > *** 1,329 **** > - -- SQL code to define the new array iterator functions and operators > - > - -- define the array operators *=, **=, *~ and **~ for type _text > - -- > - > - -- Adjust this setting to control where the objects get created. > - SET search_path = public; > - > - CREATE OR REPLACE FUNCTION array_texteq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_texteq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_textregexeq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_textregexeq(_text, text) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_text,text); > - CREATE OPERATOR *= ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_texteq > - ); > - > - DROP OPERATOR **=(_text,text); > - CREATE OPERATOR **= ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_all_texteq > - ); > - > - DROP OPERATOR *~(_text,text); > - CREATE OPERATOR *~ ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_textregexeq > - ); > - > - DROP OPERATOR **~(_text,text); > - CREATE OPERATOR **~ ( > - LEFTARG=_text, > - RIGHTARG=text, > - PROCEDURE=array_all_textregexeq > - ); > - > - > - -- define the array operators *=, **=, *~ and **~ for type _bpchar > - -- > - CREATE OR REPLACE FUNCTION array_bpchareq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_bpchareq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_bpcharregexeq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_bpcharregexeq(_bpchar, bpchar) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_bpchar,bpchar); > - CREATE OPERATOR *= ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_bpchareq > - ); > - > - DROP OPERATOR **=(_bpchar,bpchar); > - CREATE OPERATOR **= ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_all_bpchareq > - ); > - > - DROP OPERATOR *~(_bpchar,bpchar); > - CREATE OPERATOR *~ ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_bpcharregexeq > - ); > - > - DROP OPERATOR **~(_bpchar,bpchar); > - CREATE OPERATOR **~ ( > - LEFTARG=_bpchar, > - RIGHTARG=bpchar, > - PROCEDURE=array_all_bpcharregexeq > - ); > - > - > - -- define the array operators *=, **=, *> and **> for type _int4 > - -- > - CREATE OR REPLACE FUNCTION array_int4eq(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4eq(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4ne(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4ne(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4gt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4gt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4ge(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4ge(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4lt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4lt(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_int4le(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_int4le(_int4, int4) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_int4,int4); > - CREATE OPERATOR *= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4eq > - ); > - > - DROP OPERATOR **=(_int4,int4); > - CREATE OPERATOR **= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4eq > - ); > - > - DROP OPERATOR *<>(_int4,int4); > - CREATE OPERATOR *<> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4ne > - ); > - > - DROP OPERATOR **<>(_int4,int4); > - CREATE OPERATOR **<> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4ne > - ); > - > - DROP OPERATOR *>(_int4,int4); > - CREATE OPERATOR *> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4gt > - ); > - > - DROP OPERATOR **>(_int4,int4); > - CREATE OPERATOR **> ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4gt > - ); > - > - DROP OPERATOR *>=(_int4,int4); > - CREATE OPERATOR *>= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4ge > - ); > - > - DROP OPERATOR **>=(_int4,int4); > - CREATE OPERATOR **>= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4ge > - ); > - > - DROP OPERATOR *<(_int4,int4); > - CREATE OPERATOR *< ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4lt > - ); > - > - DROP OPERATOR **<(_int4,int4); > - CREATE OPERATOR **< ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4lt > - ); > - > - DROP OPERATOR *<=(_int4,int4); > - CREATE OPERATOR *<= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_int4le > - ); > - > - DROP OPERATOR **<=(_int4,int4); > - CREATE OPERATOR **<= ( > - LEFTARG=_int4, > - RIGHTARG=int4, > - PROCEDURE=array_all_int4le > - ); > - > - -- define the array operators *=, **<> for type _oid (added tobias 1. 1999) > - -- > - CREATE OR REPLACE FUNCTION array_oideq(_oid, oid) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_oidne(_oid, oid) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_oid,oid); > - CREATE OPERATOR *= ( > - LEFTARG=_oid, > - RIGHTARG=oid, > - PROCEDURE=array_oideq > - ); > - > - DROP OPERATOR **<>(_oid,oid); > - CREATE OPERATOR **<> ( > - LEFTARG=_oid, > - RIGHTARG=oid, > - PROCEDURE=array_all_oidne > - ); > - > - -- define the array operators *=, **=, *<>, **<> for type _inet > - > - CREATE OR REPLACE FUNCTION array_ineteq(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_ineteq(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_inetne(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - CREATE OR REPLACE FUNCTION array_all_inetne(_inet, inet) > - RETURNS bool > - AS 'MODULE_PATHNAME' > - LANGUAGE 'C' IMMUTABLE STRICT; > - > - DROP OPERATOR *=(_inet,inet); > - CREATE OPERATOR *= ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_ineteq > - ); > - > - DROP OPERATOR **=(_inet,inet); > - CREATE OPERATOR **= ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_all_ineteq > - ); > - > - DROP OPERATOR *<>(_inet,inet); > - CREATE OPERATOR *<> ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_inetne > - ); > - > - DROP OPERATOR **<>(_inet,inet); > - CREATE OPERATOR **<> ( > - LEFTARG=_inet, > - RIGHTARG=inet, > - PROCEDURE=array_all_inetne > - ); > --- 0 ---- > Index: contrib/Makefile > =================================================================== > RCS file: /opt/src/cvs/pgsql-server/contrib/Makefile,v > retrieving revision 1.45 > diff -c -r1.45 Makefile > *** contrib/Makefile 24 Jul 2003 16:54:58 -0000 1.45 > --- contrib/Makefile 31 Aug 2003 04:47:01 -0000 > *************** > *** 5,11 **** > include $(top_builddir)/src/Makefile.global > > WANTED_DIRS = \ > - array \ > btree_gist \ > chkpass \ > cube \ > --- 5,10 ---- > *************** > *** 44,49 **** > --- 43,49 ---- > vacuumlo > > # Missing: > + # array \ (removed all but the README) > # adddepend \ (does not have a makefile) > # ipc_check \ (does not have a makefile) > # mSQL-interface \ (requires msql installed) > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org -- Bruce Momjian | http://candle.pha.pa.us [email protected] | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073 ^ permalink raw reply [nested|flat] 280+ messages in thread
* Re: [HACKERS] New array functions @ 2003-09-15 02:52 Tom Lane <[email protected]> parent: Bruce Momjian <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Tom Lane @ 2003-09-15 02:52 UTC (permalink / raw) To: Greg Stark <[email protected]>; +Cc: Bruce Momjian <[email protected]>; Joe Conway <[email protected]>; Patches (PostgreSQL) <[email protected]> Greg Stark <[email protected]> writes: > Joe Conway wrote: >> I'll try to take a look at contrib/intarray and contrib/intagg before >> the weekend is through, and at least post a recommendation. > Can I pipe up in int_aggregate.c's defense ? I don't think we're removing it just yet. The contrib/array stuff seemed completely obsolete, but intagg isn't. regards, tom lane ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() @ 2023-01-21 00:09 Andres Freund <[email protected]> 0 siblings, 0 replies; 280+ messages in thread From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw) This just updates the places that "Zero initialize instr_time uses causing compiler warnings" changed, to see whether this is a nicer approach. --- src/include/portability/instr_time.h | 24 ++++++++++++++------ src/backend/access/transam/xlog.c | 11 +++------- src/backend/storage/buffer/bufmgr.c | 16 +++++--------- src/backend/storage/file/buffile.c | 16 +++++--------- src/backend/storage/ipc/latch.c | 8 +++---- src/bin/psql/common.c | 33 +++++++++++++--------------- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index af2ab6ec887..2d1ff4f7f82 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -17,6 +17,10 @@ * * INSTR_TIME_IS_LT(x, y) x < y * + * INSTR_TIME_ZERO() an instr_time set to 0 + * + * INSTR_TIME_CURRENT() an instr_time set to current time + * * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too) * * INSTR_TIME_SET_CURRENT(t) set t to current time @@ -110,7 +114,7 @@ typedef struct instr_time #define PG_INSTR_CLOCK CLOCK_REALTIME #endif -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_clock_gettime_ns(void) { @@ -123,8 +127,8 @@ pg_clock_gettime_ns(void) return now; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_clock_gettime_ns()) +#define INSTR_TIME_CURRENT(t) \ + pg_clock_gettime_ns() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = NS_PER_S * (s)) @@ -138,7 +142,7 @@ pg_clock_gettime_ns(void) /* Use QueryPerformanceCounter() */ -/* helper for INSTR_TIME_SET_CURRENT */ +/* helper for INSTR_TIME_CURRENT */ static inline instr_time pg_query_performance_counter(void) { @@ -160,8 +164,8 @@ GetTimerFrequency(void) return (double) f.QuadPart; } -#define INSTR_TIME_SET_CURRENT(t) \ - ((t) = pg_query_performance_counter()) +#define INSTR_TIME_CURRENT(t) \ + pg_query_performance_counter() #define INSTR_TIME_SET_SECONDS(t, s) \ ((t).ticks = s * GetTimerFrequency()) @@ -181,7 +185,13 @@ GetTimerFrequency(void) #define INSTR_TIME_IS_LT(x, y) ((x).ticks < (y).ticks) -#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0) +#define INSTR_TIME_ZERO(t) (instr_time){0} + +#define INSTR_TIME_SET_CURRENT(t) \ + (t) = INSTR_TIME_CURRENT() + +#define INSTR_TIME_SET_ZERO(t) \ + ((t) = INSTR_TIME_ZERO()) #define INSTR_TIME_SET_CURRENT_LAZY(t) \ (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fb4c860bdea..f563800c8ab 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) Size nbytes; Size nleft; int written; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; @@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) /* Measure I/O timing to write WAL data */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); written = pg_pwrite(openLogFile, from, nleft, startoffset); @@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible) */ if (track_wal_io_timing) { - instr_time duration; + instr_time duration = INSTR_TIME_CURRENT(); - INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration); } @@ -8137,7 +8134,7 @@ void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) { char *msg = NULL; - instr_time start; + instr_time start = INSTR_TIME_ZERO(); Assert(tli != 0); @@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli) /* Measure I/O timing to sync the WAL file */ if (track_wal_io_timing) INSTR_TIME_SET_CURRENT(start); - else - INSTR_TIME_SET_ZERO(start); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); switch (sync_method) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 800a4248c95..d8baf80e650 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, MemSet((char *) bufBlock, 0, BLCKSZ); else { - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); @@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) { XLogRecPtr recptr; ErrorContextCallback errcallback; - instr_time io_start, - io_time; + instr_time io_start = INSTR_TIME_ZERO(); Block bufBlock; char *bufToWrite; uint32 buf_state; @@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * bufToWrite is either the shared buffer or a copy, as appropriate. @@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 0a51624df3b..6f813279690 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -429,8 +429,7 @@ static void BufFileLoadBuffer(BufFile *file) { File thisfile; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); /* * Read whatever we can get, up to a full bufferload. @@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time); } @@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file) while (wpos < file->nbytes) { off_t availbytes; - instr_time io_start; - instr_time io_time; + instr_time io_start = INSTR_TIME_ZERO(); /* * Advance to next component file if necessary and possible. @@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); - else - INSTR_TIME_SET_ZERO(io_start); bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file) if (track_io_timing) { - INSTR_TIME_SET_CURRENT(io_time); + instr_time io_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(io_time, io_start); INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time); } diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index f4123e7de7e..8092ff4a984 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout, uint32 wait_event_info) { int returned_events = 0; - instr_time start_time; - instr_time cur_time; + instr_time start_time = INSTR_TIME_ZERO(); long cur_timeout = -1; Assert(nevents > 0); @@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout, Assert(timeout >= 0 && timeout <= INT_MAX); cur_timeout = timeout; } - else - INSTR_TIME_SET_ZERO(start_time); pgstat_report_wait_start(wait_event_info); @@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout, /* If we're not done, update cur_timeout for next iteration */ if (returned_events == 0 && timeout >= 0) { - INSTR_TIME_SET_CURRENT(cur_time); + instr_time cur_time = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(cur_time, start_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if (cur_timeout <= 0) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..5badb029e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec) bool timing = pset.timing; PGresult *result; bool OK; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); *elapsed_msec = 0; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* * To parse the query but not execute it, we prepare it, using the unnamed @@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query, { bool timing = pset.timing; bool success; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); PGresult *result; FILE *gfile_fout = NULL; bool gfile_is_pipe = false; if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); if (pset.bind_flag) success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); @@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query, */ if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec = INSTR_TIME_GET_MILLISEC(after); } @@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) int ntuples; int fetch_count; char fetch_cmd[64]; - instr_time before, - after; + instr_time before = INSTR_TIME_ZERO(); int flush_error; *elapsed_msec = 0; @@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) INSTR_TIME_SET_CURRENT(before); - else - INSTR_TIME_SET_ZERO(before); /* if we're not in a transaction, start one */ if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) @@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } @@ -1926,7 +1922,8 @@ cleanup: if (timing) { - INSTR_TIME_SET_CURRENT(after); + instr_time after = INSTR_TIME_CURRENT(); + INSTR_TIME_SUBTRACT(after, before); *elapsed_msec += INSTR_TIME_GET_MILLISEC(after); } -- 2.38.0 --vt42kbp2j7rjcapp-- ^ permalink raw reply [nested|flat] 280+ messages in thread
end of thread, other threads:[~2023-01-21 00:09 UTC | newest] Thread overview: 280+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2003-08-28 17:13 New array functions Greg Stark <[email protected]> 2003-08-28 18:15 ` Joe Conway <[email protected]> 2003-08-28 18:45 ` Greg Stark <[email protected]> 2003-08-28 19:13 ` Tom Lane <[email protected]> 2003-08-28 20:51 ` Joe Conway <[email protected]> 2003-08-28 21:05 ` Tom Lane <[email protected]> 2003-08-28 21:44 ` Joe Conway <[email protected]> 2003-08-31 04:42 ` Joe Conway <[email protected]> 2003-09-10 04:14 ` Bruce Momjian <[email protected]> 2003-09-11 17:15 ` Bruce Momjian <[email protected]> 2003-09-15 02:52 ` Tom Lane <[email protected]> 2003-08-28 20:16 ` Greg Stark <[email protected]> 2003-08-28 23:04 ` Tom Lane <[email protected]> 2003-08-28 20:52 ` Hannu Krosing <[email protected]> 2003-08-28 21:08 ` Joe Conway <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]> 2023-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