`

JVM取到的时间与操作系统时间不一致(HP-UX)

 
阅读更多

这是个有趣的问题。

我们的一个weblogic应用服务器,运行于HP-UX,启动后,发现操作系统时间与时间服务器不一致(差一分多钟),所以就把操作系统时间与时间服务器同步了一下。这时候怪事出现了,从weblogic应用中取得的时间与操作系统时间不一致了(没注意weblogic中取得的时间是否与操作系统修改之前的时间相同,待会儿验证。已经确认,目前发现HP的JDK会有这个问题,SUN的JDK没有问题)。
在网上查了一下,发现这个问题可能是由于HP-UX的JDK取时间机制造成的。

 

HP-UX SDK 1.3.1.08的release notes,里面有这么一段说明,原文如下:

date/time methods - new defaults 

Since SDK 1.2.2.09 and SDK 1.3.1, there has been a change in the way the HotSpot JVM uses the gettimeofday() system call to obtain date and time information. 

For performance reasons a new mechanism is used that uses the number of cpu ticks since the application started, to calculate the current time. 

As a result, changes to the system date or time using date(1), adjtime(2) or time synchronization utilities such as ntp will not be reflected in the date and time that Java returns, until the process is restarted. If your application requires that Java immediately reflects such system time changes, you can use the -XX:+UseGetTimeOfDay option to tell the JVM to use the gettimeofday call instead of the new, lightweight mechanism. However you may notice a drop in performance.

 
上面说的意思大致是,HP-UX SDK 1.2.2.09 和HP-UX  SDK 1.3.1之后,由于performance的原因,JVM中读取系统时间的时候,是在应用程序启动之后,读一次OS的系统时间,然后JVM中根据CPU ticks来自己计算时间,而不是实时地去OS 取系统时间。这样的话,在应用程序运行当中,如果有ntp改变了OS的系统时间,那么JVM和OS的系统时间就不一致了。只有等下一次应用程序重新启动的时候,JVM才会重新读取OS系统时间。

 

解决方案就是启动应用程序的时候,用-XX:+UseGetTimeOfDay参数,虽然这会导致performancex下降,但是可以保证JVM和OS的系统时间一致。


以前一直以为JVM是实时取OS的时间的,今天的问题再次教育了我:一切不能想当然

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics