Re: A costing analysis tool - Mailing list pgsql-hackers

From Martijn van Oosterhout
Subject Re: A costing analysis tool
Date
Msg-id 20051016121612.GA5779@svana.org
Whole thread Raw
In response to Re: A costing analysis tool  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Sat, Oct 15, 2005 at 06:48:25PM -0400, Tom Lane wrote:
> > So, what's good for battery and power usage is bad for accurate
> > timings. Basically, on Linux it would seriously underestimate the time
> > for blocking system calls on an otherwise idle system. So, it works for
> > Windows because they don't do this...
>
> Hmm ... are we *sure* they don't do that?  The QueryPerformanceCounter
> implementation was added just recently, and I'm not sure it's been
> tested under any wide range of scenarios.  Maybe we will find that it
> doesn't work :-(

I know Windows didn't used to. Remember those old Cyrix chips that were
always warm and tended to overheat. On Linux they worked great because
they were always 10+ degrees colder than when running under Windows.
There's was even a little Cyrix specific utility to enhance the effect.

The documentation [1][2] states the risk that CPU switching may cause
issues but don't mention only using it for short intervals or that the
clock may freeze. I would have thought they'd mention it if it were an
issue.

While we're on the subject I notice the code uses GetTimerFrequency()
as the baseline, whereas the documentation clearly states you should
use QueryPerformanceFrequency() which may be zero if there is no such
frequency. Note, this doesn't actually matter unless you're assuming
it's actually measuring milliseconds.

It does say the frequency can't change, which is bizarre since I'm sure
I heard about laptops running at multiple frequencies to save power,
even for Windows.

Finally, if it is using the RDTSC instruction, it won't work on
anything not Pentium class as that was the first Intel chip to have any
kind of performance counters. No idea if anyone has tested how Windows
reacts in that case or if we care, but it's something to look out for.

Here is the program [3] (only for gcc) I used to test. On my laptop it
outputs:

Using sleep() 00000074:2C039B85 - 00000074:2E34B476 (1.004 sec) = 36772081 cycles (36.614 MHz)
Busy wait     00000074:2E36A220 - 00000074:5E00CE4A (1.000 sec) = 801778730 cycles (801.778 MHz)

The first number varies considerably depending on what I'm doing at the
time. It would be interesting see how it works on other OSes, in
particular Windows.

Have a nice day,

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/queryperformancecounter.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/queryperformancefrequency.asp
[3] http://svana.org/kleptog/pgsql/rdtsc.c
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

pgsql-hackers by date:

Previous
From: Jari Aalto
Date:
Subject: FYI: PotgreSQL and SQL standards commentary WWW page
Next
From: Martijn van Oosterhout
Date:
Subject: Re: PostgreSQL roadmap for 8.2 and beyond.