• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
邹天得
博客园    首页    新随笔    联系   管理    订阅  订阅
Is System.nanoTime() completely useless?

http://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless

Is System.nanoTime() completely useless?

Ask Question
 
up vote131down votefavorite
59

As documented in the blog post Beware of System.nanoTime() in Java, on x86 systems, Java's System.nanoTime() returns the time value using a CPU specific counter. Now consider the following case I use to measure time of a call:

long time1= System.nanoTime();
foo();
long time2 = System.nanoTime();
long timeSpent = time2-time1;

Now in a multi-core system, it could be that after measuring time1, the thread is scheduled to a different processor whose counter is less than that of the previous CPU. Thus we could get a value in time2 which is less than time1. Thus we would get a negative value in timeSpent.

Considering this case, isn't it that System.nanotime is pretty much useless for now?

I know that changing the system time doesn't affect nanotime. That is not the problem I describe above. The problem is that each CPU will keep a different counter since it was turned on. This counter can be lower on the second CPU compared to the first CPU. Since the thread can be scheduled by the OS to the second CPU after getting time1, the value of timeSpent may be incorrect and even negative.

java nanotime
shareimprove this question
edited Jan 23 '15 at 11:39
Leo
4,53321639
asked Feb 4 '09 at 8:03
pdeva
7,9712688122
 
2  
I don't have an answer but I do agree with you. Maybe it should be considered a bug in the JVM. – Aaron Digulla Feb 4 '09 at 8:23
2  
that post is incorrect and not using TSC is slow but you have to live with:bugs.sun.com/bugdatabase/view_bug.do?bug_id=6440250 Also TSC can be made useful via hypervisor but then it's slow again. – bestsss Aug 12 '12 at 13:50
    
And of course, you can run in a virtual machine where a CPU can show up mid-way through a session :D – Limited Atonement Jun 18 '13 at 13:13
add a comment

14 Answers

activeoldestvotes
up vote173down voteaccepted

That post is wrong, and nanoTime is safe. There's a comment on the post which links to a blog post by David Holmes, a realtime and concurrency guy at Sun. It says:

System.nanoTime() is implemented using the QueryPerformanceCounter/QueryPerformanceFrequency API [...] The default mechanism used by QPC is determined by the Hardware Abstraction layer(HAL) [...] This default changes not only across hardware but also across OS versions. For example Windows XP Service Pack 2 changed things to use the power management timer (PMTimer) rather than the processor timestamp-counter (TSC) due to problems with the TSC not being synchronized on different processors in SMP systems, and due the fact its frequency can vary (and hence its relationship to elapsed time) based on power-management settings.

So, on Windows, this was a problem up until WinXP SP2, but it isn't now.

I can't find a part II (or more) that talks about other platforms, but that article does include a remark that Linux has encountered and solved the same problem in the same way, with a link to the FAQ for clock_gettime(CLOCK_REALTIME), which says:

  1. Is clock_gettime(CLOCK_REALTIME) consistent across all processors/cores? (Does arch matter? e.g. ppc, arm, x86, amd64, sparc).

It should or it's considered buggy.

However, on x86/x86_64, it is possible to see unsynced or variable freq TSCs cause time inconsistencies. 2.4 kernels really had no protection against this, and early 2.6 kernels didn't do too well here either. As of 2.6.18 and up the logic for detecting this is better and we'll usually fall back to a safe clocksource.

ppc always has a synced timebase, so that shouldn't be an issue.

So, if Holmes's link can be read as implying that nanoTime calls clock_gettime(CLOCK_REALTIME), then it's safe-ish as of kernel 2.6.18 on x86, and always on PowerPC (because IBM and Motorola, unlike Intel, actually know how to design microprocessors).

There's no mention of SPARC or Solaris, sadly. And of course, we have no idea what IBM JVMs do. But Sun JVMs on modern Windows and Linux get this right.

EDIT: This answer is based on the sources it cites. But i still worry that it might actually be completely wrong. Some more up-to-date information would be really valuable. I just came across to a link to afour year newer article about Linux's clocks which could be useful.

shareimprove this answer
edited Sep 16 '13 at 18:53
 
 
answered Jan 3 '11 at 21:36
Tom Anderson
31.6k85888
 
11  
Even WinXP SP2 appears to suffer. Running the original code sample, with void foo() { Thread.sleep(40); } I got a negative time (-380 ms!) using a single Athlon 64 X2 4200+ processor – Luke Usherwood Mar 7 '12 at 12:46
    
Superb answer, thanks very much – eggonlegs Sep 17 '12 at 7:06
    
I don't suppose there are updated on this, wrt. behavior on Linux, BSD or other platforms? – Tomer GabelDec 11 '13 at 14:55
2  
Good answer, should add a link to the more recent exploration of this topic:shipilev.net/blog/2014/nanotrusting-nanotime – Nitsan Wakart Mar 10 '15 at 8:31
add a comment

Did you find this question interesting? Try our newsletter

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

 
up vote34down vote

I did a bit of searching and found that if one is being pedantic then yes it might be considered useless...in particular situations...it depends on how time sensitive your requirements are...

Check out this quote from the Java Sun site:

The real-time clock and System.nanoTime() are both based on the same system call and thus the same clock.

With Java RTS, all time-based APIs (for example, Timers, Periodic Threads, Deadline Monitoring, and so forth) are based on the high-resolution timer. And, together with real-time priorities, they can ensure that the appropriate code will be executed at the right time for real-time constraints. In contrast, ordinary Java SE APIs offer just a few methods capable of handling high-resolution times, with no guarantee of execution at a given time. Using System.nanoTime() between various points in the code to perform elapsed time measurements should always be accurate.

Java also has a caveat for the nanoTime() method:

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292.3 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

It would seem that the only conclusion that can be drawn is that nanoTime() cannot be relied upon as an accurate value. As such, if you do not need to measure times that are mere nano seconds apart then this method is good enough even if the resulting returned value is negative. However, if you're needing higher precision, they appear to recommend that you use JAVA RTS.

So to answer your question...no nanoTime() is not useless....its just not the most prudent method to use in every situation.

shareimprove this answer
edited Feb 4 '09 at 14:07
J.F. Sebastian
196k48385560
answered Feb 4 '09 at 11:21
mezoid
14.7k2687141
 
2  
> this method is good enough even if the resulting returned value is negative. I dont get this, if the value in timespent is negative, then how is it useful at all in measuring the time taken in foo()? – pdeva Feb 4 '09 at 11:42
2  
its ok because all you are worried about is the absolute value of the difference. ie if your measurement is time t where t = t2 - t1 then you want to know |t|....so what if the value is negative...even with the multi core problem the impact is rarely going to be a few nanoseconds anyway. – mezoid Feb 4 '09 at 11:47
2  
To backup @Aaron: both t2 and t1 might be negative but (t2-t1) must not be negative. – J.F. Sebastian Feb 4 '09 at 14:22
2  
aaron: that is exactly what my point is. t2-t1 should never be negative otherwise we have a bug. – pdevaFeb 5 '09 at 5:07
8  
@pdeva - but you are misunderstanding what the doc says. You are raising a non-issue. There is some point in time that is considered "0". The values returned by nanoTime() are accurate relative to that time. Its a monotonically increasing timeline. You just might be getting a series of numbers from the negative portion of that timeline. -100, -99, -98 (obviously much bigger values in practice). They are going in the correct direction (increasing), so there is no issue here. – ToolmakerSteve Sep 12 '14 at 18:44
show 4 more comments
up vote16down vote

No need to debate, just use the source. Here, SE 6 for Linux, make your own conclusions:

jlong os::javaTimeMillis() {
  timeval time;
  int status = gettimeofday(&time, NULL);
  assert(status != -1, "linux error");
  return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
}


jlong os::javaTimeNanos() {
  if (Linux::supports_monotonic_clock()) {
    struct timespec tp;
    int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
    assert(status == 0, "gettime error");
    jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
    return result;
  } else {
    timeval time;
    int status = gettimeofday(&time, NULL);
    assert(status != -1, "linux error");
    jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
    return 1000 * usecs;
  }
}
shareimprove this answer
answered Apr 1 '11 at 17:07
blais
44848
 
4  
That's helpful only if you know what the used API does. The used API is implemented by the operating system; this code is correct wrt. the specification of the used API (clock_gettime/gettimeofday), but as others pointed out, some non-up-to-date operating systems have buggy implementations. – Blaisorblade Apr 30 '12 at 16:19
add a comment
up vote7down vote

You might like this better:

http://juliusdavies.ca/nanotime/

But it copies a DLL or Unix .so (shared object) file into the current user's home directory so that it can call JNI.

Some background information is on my site at:

http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html

shareimprove this answer
edited Mar 31 '10 at 15:07
 
 
answered Mar 10 '10 at 6:14
Julius Musseau
2,7151120
 
1  
+1 for referencing and appears to be correct. Welcome to stack overflow :) – Russell Mar 10 '10 at 6:21
add a comment
up vote5down vote

Absolutely not useless. Timing aficionados correctly point out the multi-core problem, but in real-word applications it is often radically better than currentTimeMillis().

When calculating graphics positions in frame refreshes nanoTime() leads to MUCH smoother motion in my program.

And I only test on multi-core machines.

shareimprove this answer
answered Mar 31 '10 at 13:27
Yuvi Masory
1,50911733
 
add a comment
up vote5down vote

Linux corrects for discrepancies between CPUs, but Windows does not. I suggest you assume System.nanoTime() is only accurate to around 1 micro-second. A simple way to get a longer timing is to call foo() 1000 or more times and divide the time by 1000.

shareimprove this answer
edited Jan 30 '14 at 15:52
Peter Mortensen
10.9k1575109
answered Feb 5 '09 at 23:17
Peter Lawrey
373k42431764
 
2  
Could you provide a reference (behaviour on Linux and Windows)? – J.F. Sebastian Feb 6 '09 at 17:07
    
Unfortunately the proposed method will commonly be highly imprecise because each event falling into the +/- 100ms wall clock update slot will often return zero for sub-second operations. The sum of 9 operations each with duration of zero is, well, zero, divided by nine is ... zero. Conversely, using System.nanoTime() will provide relatively accurate (non-zero) event durations, which then summed and divided by the number of events will provide a highly precise average. – Darrell Teague Jan 20 at 20:03 
    
@DarrellTeague summing 1000 events and adding them up is the same as the end to end time. – Peter Lawrey Jan 20 at 20:11
    
@DarrellTeague System.nanoTime() is accurate to 1 micro-second or better (not 100,000 microseconds) on most systems. Averaging many operations is only relevant when you get down to a few micro-seconds, and only on certain systems. – Peter Lawrey Jan 21 at 10:16
1  
Apologies as there was some confusion about the language used in "summing" events. Yes, if the time is marked at the beginning of say 1000 sub-second operations, they run and then time is marked again at the end and divided - that would work for some system under development to get an good approximation of duration for a given event. – Darrell Teague Jan 23 at 16:45
add a comment
up vote3down vote

I have seen a negative elapsed time reported from using System.nanoTime(). To be clear, the code in question is:

    long startNanos = System.nanoTime();

    Object returnValue = joinPoint.proceed();

    long elapsedNanos = System.nanoTime() - startNanos;

and variable 'elapsedNanos' had a negative value. (I'm positive that the intermediate call took less than 293 years as well, which is the overflow point for nanos stored in longs :)

This occurred using an IBM v1.5 JRE 64bit on IBM P690 (multi-core) hardware running AIX. I've only seen this error occur once, so it seems extremely rare. I do not know the cause - is it a hardware-specific issue, a JVM defect - I don't know. I also don't know the implications for the accuracy of nanoTime() in general.

To answer the original question, I don't think nanoTime is useless - it provides sub-millisecond timing, but there is an actual (not just theoretical) risk of it being inaccurate which you need to take into account.

shareimprove this answer
edited Jan 3 '11 at 20:54
 
 
answered Jan 3 '11 at 20:23
Basil Vandegriend
317114
 
    
Alas, seems to be some OS/hardware issue. The documentation states that the core values may be negative but (larger negative minus smaller negative) should still be a positive value. Indeed the assumption being that in the same thread, the nanoTime() call should always return a positive or negative value. Have never seen this on various Unix and Windows systems over many years but sounds possible especially if the hardware/OS are splitting this seemingly atomic operation across processors. – Darrell Teague Jan 23 at 16:49 
    
@BasilVandegriend it is not a bug anywhere. As per the documentation, rarely the second System.nanoTime() in your example can run on a different CPU and the nanoTime values calculated on that CPU could just happen to be lower than the values calculated on the first CPU. So, a -ve value for elapsedNanos is possible – endless Feb 5 at 20:12 
add a comment
up vote2down vote

This doesn't seem to be a problem on a Core 2 Duo running Windows XP and JRE 1.5.0_06.

In a test with three threads I don't see System.nanoTime() going backwards. The processors are both busy, and threads go to sleep occasionally to provoke moving threads around.

[EDIT] I would guess that it only happens on physically separate processors, i.e. that the counters are synchronized for multiple cores on the same die.

shareimprove this answer
edited Feb 4 '09 at 13:56
Aaron Digulla
212k65384597
answered Feb 4 '09 at 10:05
starblue
39.5k1166121
 
2  
It probably wont happen all the time, but due to the way nanotime() is implemented the possibility is always there. – pdeva Feb 4 '09 at 10:15
    
I would guess that it only happens on physically separate processors, i.e. that the counters are synchronized for multiple cores on the same die. – starblue Feb 4 '09 at 11:22
    
Even that depends on the specific implementation, IIRC. But that's something that the OS is supposed to take care of. – Blaisorblade Apr 30 '12 at 16:20
1  
The RDTSC counters on multiple cores of the same x86 processor are not necessarily synchronized - some modern systems let different cores run at different speeds. – Jules Apr 29 '13 at 9:51
add a comment
up vote2down vote

Java is crossplatform, and nanoTime is platform-dependent. If you use Java - when don't use nanoTime. I found real bugs across different jvm implementations with this function.

shareimprove this answer
answered Sep 21 '11 at 10:30
knave kilork
211
 
add a comment
up vote1down vote

No, it's not... It just depends on your CPU, check High Precision Event Timer for how/why things are differently treated according to CPU.

Basically, read the source of your Java and check what your version does with the function, and if it works against the CPU you will be running it on.

IBM even suggests you use it for performance benchmarking (a 2008 post, but updated).

shareimprove this answer
edited Jan 30 '14 at 15:52
Peter Mortensen
10.9k1575109
answered Feb 4 '09 at 8:13
Ric Tokyo
5,78622145
 
    
As all implementation defined bahaviour, "caveat emptor!" – David Schmitt Feb 4 '09 at 8:26
add a comment
up vote1down vote

I am linking to what essentially is the same discussion where Peter Lawrey is providing a good answer. Why I get a negative elapsed time using System.nanoTime()?

Many people mentioned that in Java System.nanoTime() could return negative time. I for apologize for repeating what other people already said.

  1. nanoTime() is not a clock but CPU cycle counter.
  2. Return value is divided by frequency to look like time.
  3. CPU frequency may fluctuate.
  4. When your thread is scheduled on another CPU, there is a chance of getting nanoTime() which results in a negative difference. That's logical. Counters across CPUs are not synchronized.
  5. In many cases, you could get quite misleading results but you wouldn't be able to tell because delta is not negative. Think about it.
  6. (unconfirmed) I think you may get a negative result even on the same CPU if instructions are reordered. To prevent that, you'd have to invoke a memory barrier serializing your instructions.

It'd be cool if System.nanoTime() returned coreID where it executed.

shareimprove this answer
answered Feb 20 '16 at 2:03
Vortex
31427
 
add a comment
up vote0down vote

The Java 5 documentation also recommends using this method for the same purpose.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.

Java 5 API Doc

shareimprove this answer
answered Feb 4 '09 at 8:31
 
Basu
 
 
add a comment
up vote0down vote

Also, System.currentTimeMillies() changes when you change your systems clock, while System.nanoTime() doesn't, so the latter is safer to measure durations.

shareimprove this answer
answered Aug 20 '15 at 12:14
RobAu
9,82533571
 
add a comment
up vote-1down vote

nanoTime is extremely insecure for timing. I tried it out on my basic primality testing algorithms and it gave answers which were literally one second apart for the same input. Don't use that ridiculous method. I need something that is more accurate and precise than get time millis, but not as bad as nanoTime.

 

posted on 2017-03-14 10:03  邹天得  阅读(524)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3