Announce: Package rdtsc for reading IA-32 time stamp counters

Hallo all, version 1.0 of package rdtsc has just been released. This small package contains one module called 'Rdtsc.Rdtsc'. This module provides the function 'rdtsc' for accessing the 'rdtsc' machine register on modern IA-32 processors. This is a 64-bit counter which counts the number of processor cycles since the machine has been powered up. Using this instruction, you can make very precise time measurements which are independent of the actual CPU frequency. But note that you can get strange results sometimes on a superscalar processor. A small program using this library looks like this: module Main where import Rdtsc.Rdtsc main = do t1 <- rdtsc t2 <- rdtsc putStrLn ("Cost of rdtsc (ffi call): " ++ show (t2 - t1)) Note that the Haskell foreign function interface imposes some additional overheads. On my machine, it takes about 950 cycles to call this function twice and to compute the difference, whereas in C the overhead is only about 84 cycles. The documentation for the package is available here: http://uebb.cs.tu-berlin.de/~magr/projects/rdtsc/ Download the source using darcs: darcs get http://uebb.cs.tu-berlin.de/~magr/darcs/rdtsc/ I would really like to get feedback on this little package, which already has been quite useful for me. Have fun and Happy Haskell Hacking in th New Year, Martin

On 1/2/07, Martin Grabmueller wrote:
version 1.0 of package rdtsc has just been released. This small package contains one module called 'Rdtsc.Rdtsc'. This module provides the function 'rdtsc' for accessing the 'rdtsc' machine register on modern IA-32 processors.
Very nice! I have a few comments: 1. What happens when someone tries this on a platform other than IA-32? I would hope for some predictable exception to be thrown (hopefully not a segfault). Hmm, checking System.Info.arch would probably cost a few cycles. I wonder how much difference that would make. Or maybe there is a CPP macro that could hide this function completely on non-IA-32? 2. Shouldn't this module be called System.Rtdsc? Or maybe even System.CPUTime.Rdtsc (even though to a systems person this is a very different animal, something feels right about that to me. I think that is where most people would look for it first.).
...note that you can get strange results sometimes on a superscalar processor.
There are a number of serious problems with using RDTSC, so anyone using it should first read up about it. In particular: using it on a very old machine could prevent Linux from booting; using it on recent high-end machines can be very inaccurate (as Martin pointed out). Microsoft "strongly discourages" its use on Windows. That said, in practice many people find RDTSC very useful. Source: http://en.wikipedia.org/wiki/RDTSC Thanks for posting this module. Regards, Yitz

magr:
Hallo all,
version 1.0 of package rdtsc has just been released.
Very nice. I've attached some small patches: a) to move the src into System.CPUTime.Rdtsc (more intuitive space) b) move C src into cbits dir (standard name) Good work, Don

Hi,
version 1.0 of package rdtsc has just been released.
This small package contains one module called 'Rdtsc.Rdtsc'.
I am wondering what it would take to get rdpmc in there as well. Of course, you'd need some way to set the pmcs before running, but that can be done using e.g. perfctr. I'd like to take a swing at implementing this, unless somebody else volunteers or thinks it's basically useless. -- Andy

Andy Georges schrieb:
Hi,
version 1.0 of package rdtsc has just been released.
This small package contains one module called 'Rdtsc.Rdtsc'.
I am wondering what it would take to get rdpmc in there as well. Of course, you'd need some way to set the pmcs before running, but that can be done using e.g. perfctr. I'd like to take a swing at implementing this, unless somebody else volunteers or thinks it's basically useless.
I welcome any patches. Currently, I do not plan to support other performance counters or measurement schemes. BTW, version 1.1 of rdtsc is now available from http://uebb.cs.tu-berlin.de/~magr/projects/rdtsc/doc/ or darcs get --partial http://uebb.cs.tu-berlin.de/~magr/projects/rdtsc/ (thanks to Bjorn Bringert for the hask-home tool used to generate the homepage for rdtsc). Bye, Martin

I would think this would be how the haskell 98 standard library CPUTime is implemented, is it not? http://haskell.org/onlinereport/cputime.html John -- John Meacham - ⑆repetae.net⑆john⑈

John Meacham wrote:
I would think this would be how the haskell 98 standard library CPUTime is implemented, is it not?
No. System.CPUTime gives you an approximate idea of the amount of CPU time your process, and all its threads, have used. The rdtsc instruction gives you a snapshot of the current CPU's cycle counter, regardless of who (if anyone) has been burning those cycles.

On Tue, Jan 23, 2007 at 04:10:10PM -0800, Bryan O'Sullivan wrote:
John Meacham wrote:
I would think this would be how the haskell 98 standard library CPUTime is implemented, is it not?
No. System.CPUTime gives you an approximate idea of the amount of CPU time your process, and all its threads, have used. The rdtsc instruction gives you a snapshot of the current CPU's cycle counter, regardless of who (if anyone) has been burning those cycles.
ah, of course. I made the same mistake before. carry on. :) the linux kernel source has readtsc equivalants for a bunch of different architectures, a simple CPP switch would allow support for most everything. John -- John Meacham - ⑆repetae.net⑆john⑈
participants (6)
-
Andy Georges
-
Bryan O'Sullivan
-
dons@cse.unsw.edu.au
-
John Meacham
-
Martin Grabmueller
-
Yitzchak Gale