Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9296c234e14ee6364ad60948032783640ce63b9d
Ticket: 965722ffffffffffffffffffffffffffffffffff
Poor time granularity on hyperthreaded CPU
User & Date: kennykb on 2004-06-03 13:17:03
Changes

  1. assignee changed to: "kennykb"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    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.
    
  4. foundin changed to: "obsolete: 8.4.6"
  5. priority changed to: "7 High"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "kennykb"
  9. subsystem changed to: "06. Time Measurement"
  10. title changed to: "Poor time granularity on hyperthreaded CPU"
  11. type changed to: "Bug"