Subject: Re: microtime(9) slow on i386?
To: None <tech-perform@netbsd.org>
From: Wolfgang Rupprecht <wolfgang+gnus20011022T140820@wsrcc.com>
List: tech-perform
Date: 10/22/2001 14:14:57
jdolecek@netbsd.org (Jaromir Dolecek) writes:
> Upon further investigation, it shows that microtime(9) on i386 uses
> port i/o, which is very slow on modern processors. Is there a way
> to improve this? This could help system overall, since microtime(9)
> is used quite often.

I was using this to profile some userland code, but a similar hack
should work in the kernel.  The most pressing gotcha is that units
output are processor clock ticks not seconds, so one needs to scale it
somewhere.

inline          u64
getTick64(void)
{
    u64             ticks;
    asm             volatile("rdtsc":"=A"(ticks));
    return ticks;
}

The nice thing about this is that it is very fast.  The count comes
from a 64-bit internal CPU register, so no external data fetch is
needed.

-wolfgang
-- 
       Wolfgang Rupprecht <wolfgang+gnus@dailyplanet.wsrcc.com>
		    http://www.wsrcc.com/wolfgang/
Coming soon: GPS mapping tools for Open Systems. http://www.gnomad-mapping.com/