[erlang-questions] System timers, now/0, and instrumentation

Amy Lear <>
Wed May 25 20:07:46 CEST 2011

On Wed, May 25, 2011 at 1:20 AM, Amy Lear <> wrote:
> On Tue, May 24, 2011 at 11:45 PM, Richard O'Keefe <> wrote:
>> Er, there's a problem with the performance counters, isn't there?
>> now() is supposed to give you a clock, something that ticks
>> regularly.
>> The performance counters count events in the CPU, and today's
>> power-sensitive CPUs do things like scaling back the CPU clock
>> frequency when they think they aren't busy.
>> I know people keep telling me that time monitoring using cpu
>> cycle counters doesn't work any more; I can't say of my own knowledge
>> whether they are right.
> According to msdn, QueryPerformanceCounter() is the solution to problems
> like RDTSC slipping in power management (
> http://msdn.microsoft.com/en-us/library/ee417693%28v=vs.85%29.aspx circa
> 2005), but some later discussions (
> http://www.red-gate.com/supportcenter/Content?p=ANTS%20Performance%20Profiler&c=ANTS_Performance_Profiler\knowledgebase\app_speedstep.htm
> ) suggest that newer power management techniques (such as AMD cool'n'quiet)
> could defeat the efforts of that timer to maintain a steady beat. It's hard
> to tell if that has been addressed by Microsoft or not since then.
> I'm really at a loss as to what the right solution here could be. I have
> trouble imagining there isn't one, but I'm not well-versed enough in the
> architecture to know where else to look.
> I do know that right now there is a higher resolution timer available than
> what now/0 uses, but I'm not clear on what the performance penalty of
> os:timestamp/0 is in comparison, and I wouldn't know how to properly measure
> it.

Okay! I've done some more research, and gotten into contact with some
folks who've had to deal with high resolution timers on multiple
platforms, and it turns out that the issues with AMD and
QueryPerformanceCounter have been resolved.

So the correct answer does appear to be to use
QueryPerformanceFrequency (
http://msdn.microsoft.com/en-us/library/ms644905.aspx ) to determine
the highest resolution clock available to you on the current machine
-- msdn claims this will never change while the machine is running --
then use QueryPerformanceCounter (
http://msdn.microsoft.com/en-us/library/ms644904 ) for the current
count. Of course this is time since system start, not a timestamp, but
determining the math needed to convert QPC beats into timestamps on
each initialization of the erlang system shouldn't be too difficult.
In theory.

I could do this as a port, but it'd be really nice if eventually now/0
could incorporate this. Using the standard instrumentation framework
would be helpful for me and I'm sure eventually someone else will be
testing on Windows as well. It's happened before, after all.

More information about the erlang-questions mailing list