Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?

From: David Geier <geidav(dot)pg(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, vignesh C <vignesh21(at)gmail(dot)com>, Lukas Fittl <lukas(at)fittl(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Ibrar Ahmed <ibrar(dot)ahmad(at)gmail(dot)com>, Maciek Sakrejda <m(dot)sakrejda(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?
Date: 2023-01-24 13:30:34
Message-ID: a9b2649b-36a0-2e69-e1fd-779e5faf4b7d@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 1/23/23 18:41, Andres Freund wrote:
> If we add it, it probably shouldn't depend on TIMING, but on
> SUMMARY. Regression test queries showing EXPLAIN ANALYZE output all do
> something like
> EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
>
> the SUMMARY OFF gets rid of the "top-level" "Planning Time" and "Execution
> Time", whereas the TIMING OFF gets rid of the per-node timing. Those are
> separate options because per-node timing is problematic performance-wise
> (right now), but whole-query timing rarely is.
Makes sense. I wasn't aware of SUMMARY. Let's keep this option in mind,
in case we'll revisit exposing the clock source in the future.
> Another, independent, thing worth thinking about: I think we might want to
> expose both rdtsc and rdtscp. For something like
> InstrStartNode()/InstrStopNode(), avoiding the "one-way barrier" of rdtscp is
> quite important to avoid changing the query performance. But for measuring
> whole-query time, we likely want to measure the actual time.
>
> It probably won't matter hugely for the whole query time - the out of order
> window of modern CPUs is large, but not *that* large - but I don't think we
> can generally assume that.

That's what I thought as well. I added INSTR_TIME_SET_CURRENT_FAST() and
for now call that variant from InstrStartNode(), InstrEndNode() and
pg_test_timing. To do so in InstrEndNode(), I removed
INSTR_TIME_SET_CURRENT_LAZY(). Otherwise, two variants of that macro
would be needed. INSTR_TIME_SET_CURRENT_LAZY() was only used in a single
place and the code is more readable that way. INSTR_TIME_SET_CURRENT()
is called from a bunch of places. I still have to go through all of them
and see which should be changed to call the _FAST() variant.

Attached is v7 of the patch:

- Rebased on latest master (most importantly on top of the int64
instr_time commits). - Includes two commits from Andres which introduce
INSTR_TIME_SET_SECONDS(), INSTR_TIME_IS_LT() and WIP to report
pg_test_timing output in nanoseconds. - Converts ticks to nanoseconds
only with integer math, while accounting for overflow. - Supports RDTSCP
via INSTR_TIME_SET_CURRENT() and introduced
INSTR_TIME_SET_CURRENT_FAST() which uses RDTSC.

I haven't gotten to the following:

- Looking through all calls to INSTR_TIME_SET_CURRENT() and check if
they should be replaced by INSTR_TIME_SET_CURRENT_FAST(). - Reviewing
Andres commits. Potentially improving on pg_test_timing's output. -
Looking at enabling RDTSC on more platforms. Is there a minimum set of
platforms we would like support for? Windows should be easy. That would
also allow to unify the code a little more. - Add more documentation and
do more testing around the calls to CPUID. - Profiling and optimizing
the code. A quick test showed about 10% improvement over master with
TIMING ON vs TIMING OFF, when using the test-case from Andres' e-mail
that started this thread.

I hope I'll find time to work on these points during the next days.

--
David Geier
(ServiceNow)

Attachment Content-Type Size
v7-0001-instr_time-Add-INSTR_TIME_SET_SECONDS-INSTR_TIME_.patch text/x-patch 2.0 KB
v7-0002-wip-report-nanoseconds-in-pg_test_timing.patch text/x-patch 11.3 KB
v7-0003-Use-RDTSC-P-instructions-to-measure-time-on-x86-L.patch text/x-patch 17.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2023-01-24 13:35:45 Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?
Previous Message Bharath Rupireddy 2023-01-24 13:30:00 Re: WAL Insertion Lock Improvements (was: Re: Avoid LWLockWaitForVar() for currently held WAL insertion lock in WaitXLogInsertionsToFinish())