Tcl Source Code

View Ticket
Login
Ticket UUID: 1821187
Title: extend TclpGetWideClicks to other platforms
Type: RFE Created on: 2007-10-27 12:08:24
Submitter: msofer Assigned to: kennykb
Subsystem: 06. Time Measurement Severity:
Priority: 5 Medium Last modified: 2007-11-06 12:26:59
Status: Open Closed by:
Resolution: None Closed on:
Version: None
Description:
When working on performance measurements of tiny bites of code (eg, individual instructions in TEBC) the resolution of TclpGetClicks() is sometimes insufficient. But the platform may have click sources of higher resolution.

* OSX has TclpGetWideClicks which (I am assuming) has higher resolution

* Windows has the QueryPerformanceCounter function, which is used elsewhere in tclWinTime.c but not for this purpose

* on Pentium and later x86 cpus the rdtsc opcode is available. On linux, the rdtscll function may or may not be available depending on kernel config, but we can define it as

#define GetClicks(foo) do {\
   uint32_t __a,__d; \
   asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
   (foo) = ((unint64_t)__a) | (((uint64_t)__d)<<32); \
} while(0)

* other platforms?

The last two solutions (OSX too?) would require that the process be confined to a single cpu to insure accuracy, but we can leave that to the user.
User Comments:
das added on 2007-11-06 12:26:59:
Logged In: YES 
user_id=90580
Originator: NO

On OSX, TclpGetWideClicks() is based on mach_absolute_time() which is the highest resolution timer available on OSX in a processor-independent fashion. It runs at the CPU bus frequency and is incremented independently of multiple cores/CPUs or speedstep and the like. The frequency can be determined via mach_timebase_info(), allowing conversion of wide clicks to nanonseconds in TclpWideClicksToNanoseconds()  - which is used by [time] on OSX (for docs c.f. http://developer.apple.com/qa/qa2004/qa1398.html).
There are higher resolution processor-specific timers available, e.g. on the intel pentium m family there are the PMCs CPU_CLK_UNHALTED and CPU_CLK_UNHALTED.BUS (the latter independent of speedstep), my tclCHUDtime.tcl extension uses these to do higher-resolution timing, also c.f. there for the code to convert ticks of these timers to nanoseconds: http://rutherglen.ics.mq.edu.au/~steffen/tcltk/carboncritlib/tclCHUDtime.tcl