Skip to content
Tcl Source Code History Of Ticket 965722ffffffffffffffffffffffffffffffffff
Login

History Of Ticket 965722ffffffffffffffffffffffffffffffffff

Artifacts Associated With Ticket 965722ffffffffffffffffffffffffffffffffff

  1. Ticket change [9296c234e1] (rid 120786) by kennykb on 2004-06-03 13:17:03:

    1. assignee initialized to: "kennykb"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized 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 initialized to: "obsolete: 8.4.6"
    5. priority initialized to: "7 High"
    6. resolution initialized to: "None"
    7. status initialized to: "Open"
    8. submitter initialized to: "kennykb"
    9. subsystem initialized to: "06. Time Measurement"
    10. title initialized to: "Poor time granularity on hyperthreaded CPU"
    11. type initialized to: "Bug"
  2. Ticket change [8c7a9851bf] (rid 127750) by kennykb on 2004-06-06 00:39:38:

    1. closedate changed to: "2004-06-05T17:39:38"
    2. closer changed to: "kennykb"
    3. icomment:
      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.
      
      
    4. login: "kennykb"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
    8. username: "kennykb"