Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > System.nanoTime and multiple cpus/cores

Reply
Thread Tools

System.nanoTime and multiple cpus/cores

 
 
transpendence@googlemail.com
Guest
Posts: n/a
 
      03-17-2006
I've tried to use System.nanoTime to make precise measures of timing
intervalls. I't works great - but only as long as the program runs on
one cpu only. If there are multiple cpus/cores, the running thread seem
to switch between different cpus and each cpu seem to have a different
timer base the result of nanoTime is jumping forward and backward in
time, depending on which cpu the thread is currently running.

Is there a possibility to force threads to a single cpu directly in
java or to use another high-precession timer (I need ms resolution and
it should work on Windows too)?

 
Reply With Quote
 
 
 
 
Hendrik Maryns
Guest
Posts: n/a
 
      03-17-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

http://www.velocityreviews.com/forums/(E-Mail Removed) schreef:
> I've tried to use System.nanoTime to make precise measures of timing
> intervalls. I't works great - but only as long as the program runs on
> one cpu only. If there are multiple cpus/cores, the running thread seem
> to switch between different cpus and each cpu seem to have a different
> timer base the result of nanoTime is jumping forward and backward in
> time, depending on which cpu the thread is currently running.


Are you sure it is due to multithreading? There are these small
particles that are known to be able to jump back in time (have to do
some reading up on quantummechanics before posting stuff like this).



H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEGsLYe+7xMGD3itQRArujAJwLq+QEZkwnNb5r7tISeT E8hqxuggCcDDdd
tFZAXTV8d8cHFiIhlNCoF6w=
=eMGQ
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
lewmania942@yahoo.fr
Guest
Posts: n/a
 
      03-17-2006
(E-Mail Removed) wrote:
> I've tried to use System.nanoTime to make precise measures of timing
> intervalls. I't works great - but only as long as the program runs on
> one cpu only.


I recall an excellent thread (was it an article?) explaining why
you can't rely on System.nanoTime() to give ultra-precise results
when running on multi-cpu/cores... But can't find it back

You can't entirely reject the possibility of an error in the value
given back by System.nanoTime() either: if you search the
web you'll find at least one such bug (I think it was one
version of Windows that was at fault).


> Is there a possibility to force threads to a single cpu directly in
> java


Directly in Java no, even if years ago there have been talks
about this at Sun. They said that "maybe one day" you'd have
the ability to call a method like:

setCPUAffinity()

to define the "affinity mask". This would have solved your
problem but AFAIK is has never been implemented (and may
not even be possible to implement at the JVM level).

That said, maybe you can force the affinity mask at the OS level
if you *really* need it (I'd rather let the OS scheduler decide
how the cores/cpus are used).

 
Reply With Quote
 
lewmania942@yahoo.fr
Guest
Posts: n/a
 
      03-17-2006
> > If there are multiple cpus/cores, the running thread seem
> > to switch between different cpus and each cpu seem to have a different
> > timer base the result of nanoTime is jumping forward and backward in
> > time, depending on which cpu the thread is currently running.

>
> Are you sure it is due to multithreading? There are these small
> particles that are known to be able to jump back in time (have to do
> some reading up on quantummechanics before posting stuff like this).
>
>




But... Don't you think his explanation may be *exactly* what
is going on? To me nanoTime() is not very precise when
running with several cores/cpus, so the OP's explanation doesn't
seem far-fetched at all (but I may be wrong).

Heck, even the famous assembly "rdtsc" instruction (mentionned
on Roedy's site btw) could only be used to measure timing
accurately if and only if the pipeline was flushed, to prevent
out-of-order instructions execution. This required hacks and...
serious performance drops (flushing the pipeline could be done
by using cpuid).

That said, I can't wait to have Java 9874 which implements
System.picoTime(): this time it *really* is accurate... Then two
months later Intel starts selling the new virtual-multi-transparent
-woozing-buzz-architreadhed-cored-processor and picoTime()
isn't really that precise anymore. Repeat ad nauseam.



Not that I personnally need sub-nanosecond precision timer
or anything

 
Reply With Quote
 
transpendence@googlemail.com
Guest
Posts: n/a
 
      03-17-2006
I can force it to a single cpu via the windows task manager (the
problems are gone then) - but only after the program has started. And
it limits the whole process to a single cpu.

But it seems I've found a solution:

After some searching, I've found that System.nanoTime() uses
QueryPerformanceCounters() on Windows and that this function is known
to have problems on Athlon64 multicore systems. I've found a hint to
use /usepmtimer in win.ini. Don't know if it really always works, but
after I changed it, the problems are gone.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-17-2006
On 17 Mar 2006 05:53:03 -0800, (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>I've tried to use System.nanoTime to make precise measures of timing
>intervalls. I't works great - but only as long as the program runs on
>one cpu only. If there are multiple cpus/cores, the running thread seem
>to switch between different cpus and each cpu seem to have a different
>timer base the result of nanoTime is jumping forward and backward in
>time, depending on which cpu the thread is currently running.


that's a bug. Java is supposed to compensate for that.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-17-2006
Roedy Green wrote:
> On 17 Mar 2006 05:53:03 -0800, (E-Mail Removed) wrote,
> quoted or indirectly quoted someone who said :
>
>
>>I've tried to use System.nanoTime to make precise measures of timing
>>intervalls. I't works great - but only as long as the program runs on
>>one cpu only. If there are multiple cpus/cores, the running thread seem
>>to switch between different cpus and each cpu seem to have a different
>>timer base the result of nanoTime is jumping forward and backward in
>>time, depending on which cpu the thread is currently running.

>
>
> that's a bug. Java is supposed to compensate for that.


I agree its a bug, but I'm not sure Java can compensate for it. The JVM
does not necessarily know when a thread moves. The operating system does
know, and should be providing a consistent timer at the syscall, or
equivalent, level.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-17-2006
On Fri, 17 Mar 2006 19:26:30 GMT, Patricia Shanahan <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>I agree its a bug, but I'm not sure Java can compensate for it. The JVM
>does not necessarily know when a thread moves. The operating system does
>know, and should be providing a consistent timer at the syscall, or
>equivalent, level.


hmm. You would have to enqueue a request to a fixed timer thread.
That of course defeats the fine grain resolution.

Is there at least an integer index of CPU you could grab at the same
time as the RDTSC? Intels have a serial number, which can be
disabled. AMDs don't.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
lewmania942@yahoo.fr
Guest
Posts: n/a
 
      03-18-2006

> hmm. You would have to enqueue a request to a fixed timer thread.
> That of course defeats the fine grain resolution.


Indeed.

Do you know how I can find (out of curiosity) how nanoTime() is
implemented in Java 1.5 (and/or 1.6) ? (Say under Windows XP
and under Linux).

RDTSC is flawed anyway... As I wrote in another post in this thread,
to have a "real" fine-grained RDTSC you have to flush the pipeline
before using the instruction, which in itself kind of defeats the
purpose.

Moreover with all the CPU that throttle their speed (such as many
Notebook CPUs) RDTSC is basically useless.

And apparently on some hyper-threading systems, methods like
Window's QueryPerformanceCounter sometimes falls back to
RDTSC...

This is giving headaches to many game programmers

 
Reply With Quote
 
lewmania942@yahoo.fr
Guest
Posts: n/a
 
      03-18-2006
Hi Patricia,

Patricia Shanahan wrote:
....
> I agree its a bug, but I'm not sure Java can compensate for it. The JVM
> does not necessarily know when a thread moves. The operating system does
> know, and should be providing a consistent timer at the syscall, or
> equivalent, level.


but apparently doesn't provide it.



I found back a thread from 2003 on an Intel forum... (I'm pretty sure
the hundreds-of-mega-bytes of patch Windows has had since that time
didn't fix that problem and the situation on Linux OSes doesn't seem
any better

http://softwareforums.intel.com/ids/...message.id=155

"You could set up the OS to support high precision
"virtual timers or virtual TSC's (it's fairly trivial)
"but it's not currently there in any OS

Now I'm all ears: if someone can show me how to cleanly have a
Java high precision timer on a multi-cored-multi-cpu-hyper-
threaded-(insert latest CPU feature)-system providing nanosecond
(or sub-nanosecond) accuracy without side effect (for example
without flushing any pipeline), I'll read very carefully. It has to
work on Intel, AMDs, and all the others and also, of course, on
various OSes.

Until then, I'll code my Java apps without relying on System.nanoTime()
giving very meaningfull values (ie: without hoping it'll really provide
a high-precision timer on the various architectures the JVM run on)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple ISPs and Multiple IP Ranges from Each ISP Chennak Cisco 10 06-08-2005 09:29 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
running multiple threads and multiple processes at the same time Smegly C Programming 1 05-19-2004 02:59 AM
Multiple forms and Multiple submit buttons and validation sandyde2 Javascript 2 10-02-2003 09:25 AM
Re: One solution containing multiple projects and multiple hosts Stephen Miller ASP .Net 0 06-24-2003 12:55 PM



Advertisments