Bug#404989: gij-4.1: System.nanoTime() goes backwards
Package: gij-4.1
Version: 4.1.1-17
Severity: normal
-- System Information:
Debian Release: 4.0
APT prefers testing
APT policy: (500, 'testing')
Architecture: i386 (i686)
Shell: /bin/sh linked to /bin/bash
Kernel: Linux 2.6.18-3-686
Locale: LANG=en_US, LC_CTYPE=en_US (charmap=ISO-8859-1)
Versions of packages gij-4.1 depends on:
ii gcj-4.1-base 4.1.1-17 The GNU Compiler Collection (gcj b
ii libc6 2.3.6.ds1-8 GNU C Library: Shared libraries
ii libgcc1 1:4.1.1-19 GCC support library
ii libgcj7-0 4.1.1-17 Java runtime library for use with
ii zlib1g 1:1.2.3-13 compression library - runtime
gij-4.1 recommends no packages.
-- no debconf information
Sometimes System.nanoTime() reports time stamps that go backwards.
My test case is the following:
----- test case 1 -----
public class test1 {
public static void main(String[] args) {
long ts1;
long ts2;
do {
ts1 = System.nanoTime();
ts2 = System.nanoTime();
} while (ts1 <= ts2);
System.out.println("TS1: " + ts1 + ", TS2: " + ts2);
}
}
-----------------------
1) Compile the Java source using the Sun compiler on a Debian 'unstable' system (i686).
$ javac test1.java
$
$ dpkg -l sun-java5-jdk
ii sun-java5-jdk 1.5.0-10-1.1 Sun Java(TM) Development Kit (JDK) 5.0
$
$ java -version
java version "1.5.0_10"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03)
Java HotSpot(TM) Client VM (build 1.5.0_10-b03, mixed mode, sharing)
2) Run the program on this Debian 'testing' system (i686) using gij.
$ java -version
java version "1.4.2"
gij (GNU libgcj) version 4.1.2 20061020 (prerelease) (Debian 4.1.1-17)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
$ java test1
TS1: 1516335999794, TS2: 1515337000426
This may take some seconds to show up, but it indicates that the second time stamp
is before the first time stamp.
I think System.nanoTime() is implemented on top of the C function clock_gettime() but
uses the returned information with wrong alignment (clock_gettime() returns nano-seconds
not micro-seconds). Test case:
----- test case 2 -----
public class test2 {
public static void main(String[] args) {
System.out.println(System.nanoTime());
}
}
-----------------------
Compiling this program as before and using strace (strace -o /tmp/test2.strace java test2) shows:
...
...
mmap2(NULL, 12288, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6209000
clock_gettime(CLOCK_MONOTONIC, {1515537, 182512429}) = 0
write(1, "1515719512429\n", 14) = 14
exit_group(0) = ?
While clock_gettime() returns (1515537, 182512429), the System.nanoTime() gives 1515719512429.
This is an alignment error:
clock_gettime:
1515537.000000000
.182512429
-----------------
1515537.182512429
System.nanoTime():
1515.537000000
.182512429
-----------------
1515.719512429
Best regards,
-andi
Reply to: