Andy Malakov software blog

Thursday, July 31, 2008

A bug in Sun's 64-bit hotspot compiler

A colleague of mine found a scary bug in Sun's hotspot compiler (64 bit version). It is reproducible on several computers in our office (CPUs: 2x AMD Opteron, Intel E8400; OS: Windows XP Professional 64-bit). Here is the code:

public class FailingTest {

private final java.io.RandomAccessFile raf;
private long sequence;

public FailingTest() throws Exception {
java.io.File f = new java.io.File("test.dat");
f.delete();
raf = new java.io.RandomAccessFile(f, "rw");
f.deleteOnExit();
}

public void test(long timestamp) throws Exception {
// This condition never happens but is required to
// reproduce this bug, so is unused timestamp argument
if (timestamp < 0)
System.exit(0);

final long valueToStore = sequence++;
raf.seek(0);
raf.writeLong(valueToStore);
raf.seek(0);
final long valueRead = raf.readLong();
if (valueRead != valueToStore)
System.err.println("Error: Read value " +
valueRead + " != " + valueToStore);
}

public static void main (String [] args) throws Exception {
FailingTest t = new FailingTest();
for (int ii = 0; ii < 1000000; ii++)
t.test(ii);
}
}

On my machine this test fails with the following output (I run it with -server -XX:CompileThreshold=100):

Error: Read value 4826 != 4825

The test fails every time with different numbers, with any version of 64-bit Sun JDK 1.6 we tried. This problem is NOT reproducible under 32-bit version and NOT reproducible on BEA's JRockit 1.6 (64 bit). Just verified that problem remains on the latest JVM available: build 1.6.0_10-beta-b25, Java HotSpot(TM) 64-Bit Server VM (build 11.0-b12, mixed mode).

Monday, July 21, 2008

Debug mode penalty in .NET and Java

Java users quite often distribute their code compiled in debug mode. This improves error diagnostics while performance penalty for having debug information in class files is negligible. Java doesn't have compile-time optimization since version 1.3 (for 8 years by now).

That is why I was amazed to see that C# Debug version of my test was 14.5 times slower compared to Release (compiled as /optimize+ /checked- /debug-). I could have imagined such difference for C++, but not for C# that also has just-in-time (JIT) optimization at run-time.

Monday, July 7, 2008