|
2004-06-06
| ||
| 00:39 | • Closed ticket [965722ffff]: Poor time granularity on hyperthreaded CPU plus ... artifact: 8c7a9851bf user: kennykb | |
|
2004-06-03
| ||
| 13:17 | • New ticket [965722ffff]. artifact: 9296c234e1 user: kennykb | |
Poor time granularity on hyperthreaded CPU
Closed / Fixed965722ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 22 years ago
Descriptionplain2004-06-03 13:17:03
Jeff Godfrey wrote:
> I'm trying to do some animation on a canvas, and I
need a high-resolution
> timer in order to keep the frame-rates consistent
across varying machines.
> I went through some troubles with the resolution of
[clock
> clicks -milliseconds] a number of years ago on Win98,
but Kevin Kenny was
> able to supply a core patch to fix that problem.
Tcl is close to the ragged edge of what's possible on
Windows,
I'm afraid. Let me give a little bit of background
here, and
maybe the Windows experts here can figure out what's
The Right
Thing.
You will notice that Tcl actually gives you timing to
submillisecond accuracy on many Windows machines. As far
as I know, *no other product attempts this feat without
additional hardware support.* How do we do it? We use a
phase-locked loop to discipline the system performance
counter (which is uncalibrated) to the system clock,
allowing us to interpolate between ticks of the clock.
Alas, the system performance counter is at best imperfect.
Reading the documentation for QueryPerformanceCounter,
http://tinyurl.com/3xp96
we find the bizarre remark (which has been there since
at least
NT 4.0):
On a multiprocessor machine, it should not matter
which processor is called. However, you can get
different results on different processors due to
bugs in the basic input/output system (BIOS) or the
hardware abstraction layer (HAL).
In fact, I am well aware of the problem. At least one HAL
for multiprocessor machines implemented the performance
counter
with a simple RDTSC instruction, and derived the clock
references for the two CPU's from separate crystals. This
limitation makes the performance counter essentially
useless
unless you set the affinity mask of the thread that's using
it to constrain it to one or the other CPU.
Tcl detects systems that have the problem by a simple
check.
The initialization code in Tcl_GetTime (tclWinTime.c,
starting
around line 282) looks at the performance counter
frequency.
If it's at most 15 MHz, it's presumed to be derived
from the
8254-compatible real-time clock, and Tcl uses the
performance
counter to interpolate between ticks of the system clock.
Otherwise, it's presumed to be the result of RDTSC, or
of the
ACPI power timer, and is considered untrustworthy. If the
performance counter is untrustworthy, the code falls
back on
using the system clock, which has a much coarser
granularity
(up to 20 ms on modern Windows; some versions of Win95 used
55 ms).
This heuristic has been "good enough" in 8.4; it
captured by
far the greater number of desktops and laptops, failing
only
on a small number of server-class machines. The advent of
hyperthreading has changed all that. On a
hyperthreaded machine,
the performance counter is usually derived using RDTSC.
Now,
however, both CPU's are aboard the same chip, and share
a common
timestamp counter (or perhaps have separate TSC
registers that
increment in lockstep - essentially the same thing for the
purposes of this discussion). Now we have a large class of
machines on which QueryPerformanceFrequency will be a
couple
of GHz, and the performance counter *is* reliable.
Conservatism is best here. If we use an unreliable
performance counter, we return wildly incorrect times
to the
script. If we fail to use a reliable one, we suffer at
worst
from imprecision. So what I need is a reliable way to test
whether the performance counter may, in fact, be relied on.
I can manage to grind my way through decoding the
information
from CPUID if I must. However, that will require a short
codeburst in assembly language. I'm a trifle reluctant to
introduce that code owing to the obvious maintainability
issues - at the very least, it needs to support both
gcc and
VC++, whose assemblers are very different. If there's
something
off the shelf in Windows that I can use to get the
information,
I've not been able to find it.
kennykbplain2004-06-06 00:39:38
Logged In: YES user_id=99768 Jeff, could you please try this again against either the HEAD or the tip of core-8-4-branch ? I *think* I've a fix in there that will work in your situation, but I don't have an HT machine to test on.
Log in (anonymous is fine) to add a comment.