Re: rdtsc and the sleep fcn (Was problem with rdtsc instruction on Pentium M)

From: Michael Brown (spamtrap_at_crayne.org)
Date: 10/29/04

  • Next message: Vasanth Venkatachalam: "Re: rdtsc and the sleep fcn (Was problem with rdtsc instruction on Pentium M)"
    Date: Thu, 28 Oct 2004 22:10:27 +0000 (UTC)
    
    

    John Doe wrote:
    > I ran some more tests with rdtsc. It turns out that when I use it to
    > time ordinary programs the numbers are not that different from those
    > obtained by the time( ) function. It is only when I time the sleep( )
    > function that the numbers are off by a maginitude of seconds, far
    > less than they should be.
    >
    > So my hunch is that the timestamp counter on my Pentium M stops
    > counting cycles during part of the sleep( ) function. Maybe this
    > function goes into the idle loop, causing the tsc to stop counting.

    This is correct (as have been mentioned many times). Any halfway decent
    modern OS's sleep function won't sit in a loop. It marks the thread as
    sleeping, puts a notification in a queue to wake it up at some later point
    in time, then goes off and does something else for a while (runs another
    thread, idles, etc). As part of idleing, the CPU is slowed down which stuffs
    up the TSC.

    > Is there any workaround for this in the OS?

    The solution is not to use RDTSC. The alternatives depend on the OS you are
    using, which by your newsreader I'm guessing is Windows. In this case, use
    the QueryPerformanceCounter function for high-resolution timing. On modern
    mobile (ie: non TSC-stable systems) it uses the ACPI timer which has a
    constant frequency regardless of the CPU frequency. IIRC the utime function
    does the same thing on Linux (when ACPI support is compiled in, of course).

    > I read that someone in
    > the previous post designed their own idle loop to prevent the CPU
    > from going into the main idle loop.
    >
    > I'm looking for a similar solution, but I don't want the processor to
    > be doing unnecessary work when it is supposed to idle. (I'll be
    > exploiting idle periods for power management.)

    In that case you simply cannot use RDTSC. The primary way of saving power is
    to throttle down the CPU, which disturbs the TSC.

    [...]

    --
    Michael Brown
    www.emboss.co.nz : OOS/RSI software and more :)
    Add michael@ to emboss.co.nz - My inbox is always open
    

  • Next message: Vasanth Venkatachalam: "Re: rdtsc and the sleep fcn (Was problem with rdtsc instruction on Pentium M)"

    Relevant Pages

    • Re: [patch] x86: unify/rewrite SMP TSC sync code
      ... We shouldnt really trust the CPU and the board/BIOS ... from one CPU to another within a critical section), then the TSC ... There was a patch from google for trap -- trapping RDTSC for syncing ... and we should rather take a look at /why/ these apps are ...
      (Linux-Kernel)
    • Re: [patch] x86: unify/rewrite SMP TSC sync code
      ... after the fact - what if the app already read out an older TSC value and a new CPU is added. ... If the TSC isnt sync on SMP then it quickly gets pretty messy, and we should rather take a look at /why/ these apps are using RDTSC. ...
      (Linux-Kernel)
    • Re: System over heating
      ... reinstall XP Pro on it yesterday and now I am having this really strange problem with the over heating. ... computing cores can run at a safe temp when the CPU is running at ... There is yet another reason that a laptop can run warmer than normal. ... On my computer, the idle loop uses the HLT instruction, ...
      (microsoft.public.windowsxp.hardware)
    • Re: Word 2004 taking 30% CPU even when idle w/ no doc open
      ... Word is an "idle loop processor". ... quite different from "30 per cent of the available CPU". ... using the combo update in safe mode. ...
      (microsoft.public.mac.office.word)
    • Re: what exactly does CPU_TIME measure?
      ... clock cycles then rdtsc makes more sense. ... speed CPU) but clock cycles are. ... so you can get accurate timing even for small ...
      (comp.lang.fortran)