Re: What is a typical precision of gettimeofday()?

From: Hannu Krosing <hannuk(at)google(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, "Andrey M(dot) Borodin" <x4mmm(at)yandex-team(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: What is a typical precision of gettimeofday()?
Date: 2024-07-02 17:31:21
Message-ID: CAMT0RQTzzzSfO4j3PeF5riQ_mER_9BE_LNnjEUrM349ZhhDbww@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Tom,

On various Intel CPUs I got either steps close to single nanosecond or
sometimes a little more on older ones

One specific CPU moved in in 2 tick increments while the ration to ns
was 2,1/1 or 2100 ticks per microsecond.

On Zen4 AMD the step seems to be 10 ns, even though the tick-to-ns
ratio is 2.6 / 1 , so reading ticks directly gives 26, 54, ...

Also, reading directly in ticks on M1 gave "loop time including
overhead: 2.13 ns" (attached code works on Clang, not sure about GCC)

I'll also take a look at the docs and try to propose something

Do we also need tests for this one ?

----
Hannu

On Tue, Jul 2, 2024 at 7:20 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> BTW, getting back to the original point of the thread: I duplicated
> Hannu's result showing that on Apple M1 the clock tick seems to be
> about 40ns. But look at what I got with the v2 patch on my main
> workstation (full output attached):
>
> $ ./pg_test_timing
> ...
> Per loop time including overhead: 16.60 ns
> ...
> Timing durations less than 128 ns:
> ns % of total running % count
> 15 3.2738 3.2738 5914914
> 16 49.0772 52.3510 88668783
> 17 36.4662 88.8172 65884173
> 18 9.5639 98.3810 17279249
> 19 1.5746 99.9556 2844873
> 20 0.0416 99.9972 75125
> 21 0.0004 99.9976 757
> ...
>
> It sure looks like this is exact-to-the-nanosecond results,
> since the modal values match the overall per-loop timing,
> and there are no zero measurements.
>
> This is a Dell tower from 2021, running RHEL8 on an Intel Xeon W-2245.
> Not exactly top-of-the-line stuff.
>
> regards, tom lane
>

Attachment Content-Type Size
pg_test_ticking.c text/x-csrc 6.9 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 2024-07-02 17:37:35 Re: What is a typical precision of gettimeofday()?
Previous Message Robert Haas 2024-07-02 17:24:44 Re: On disable_cost