Categories
Java Software Technology

Java 10 HotSpot Disassembly on macOS High Sierra

Printing Generated Assembly Code From The Hotspot JIT Compiler documented back in 2013 how to view Java Hotspot generated assembly code.

While still useful, the disassembler plugin referenced in the post is no longer available in binary form as the Kenai project has been decommissioned.

A number of references are available on how to build the plugin, however information on how to build on current macOS systems is hard to come by. Here is how to build the disassembler plugin on Java 10.

Prerequisites
  • macOS High Sierra 10.13
  • Xcode 9.3 (including Command-line Tools)
Instructions
#!/bin/bash -e
# Download OpenJDK Reference Implementation Sources from
# http://jdk.java.net/java-se-ri/10
curl -O https://download.java.net/openjdk/jdk10/ri/openjdk-10_src.zip
# Navigate to the hsdis sources
unzip openjdk-10_src.zip
cd openjdk/src/utils/hsdis
# Download binutils 2.26
curl -O https://mirrors.syringanetworks.net/gnu/binutils/binutils-2.26.tar.gz
tar xzvf binutils-2.26.tar.gz
# Build hsdis
make BINUTILS=binutils-2.26 all64
# Install hsdis
sudo cp build/macosx-amd64/hsdis-amd64.dylib /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/server

view raw
build-hsdis-macos.sh
hosted with ❤ by GitHub

Links
  • https://github.com/AdoptOpenJDK/jitwatch/wiki/Building-hsdis pointed out the requirement for binptils 2.26
  • https://www.chrisnewland.com/updated-instructions-for-building-hsdis-on-osx-417 was a good starting point
  • OpenJDK Supported platforms: https://wiki.openjdk.java.net/display/Build/Supported+Build+Platforms
  • OpenJDK Sources: http://jdk.java.net/java-se-ri/10
  • java command line arguments: https://docs.oracle.com/javase/10/tools/java.htm

 

Categories
Java Software

The Cost of Contention

Martin Thompson first reported on the cost of contention using a simple benchmark that measures the time to increment a 64-bit counter 500 million times using various strategies. Results were reported here (section 3.1) and here (Managing Contention vs. Doing Real Work).

I re-implemented this benchmark here.

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestIncrement {
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
runTest(new CounterBare(), "Single thread", 1);
runTest(new CounterWithVolatile(), "Single thread with volatile", 1);
runTest(new CounterWithCAS(), "Single thread with CAS", 1);
runTest(new CounterWithSynchronized(), "Single thread with synchronized", 1);
runTest(new CounterWithLock(), "Single thread with lock", 1);
runTest(new CounterWithCAS(), "Two threads with CAS", 2);
runTest(new CounterWithSynchronized(), "Two threads with synchronized", 2);
runTest(new CounterWithLock(), "Two threads with lock", 2);
}
private static void runTest(final Counter counter, final String name, final int threadCount)
throws InterruptedException, BrokenBarrierException {
final CountDownLatch endLatch = new CountDownLatch(threadCount);
final int warmUpIterations = 100_000;
final int iterations = 500_000_000;
final int perThreadIterations = iterations / threadCount;
final CyclicBarrier startBarrier = new CyclicBarrier(threadCount + 1);
for (int i = 0; i < threadCount; i++) {
@SuppressWarnings("Convert2Lambda")
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int a = 0; a < warmUpIterations; a++) {
runIterations(counter, 5);
}
for (int a = 0; a < 5; a++) {
runIterations(counter, warmUpIterations);
}
try {
startBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
throw new IllegalStateException(e);
}
runIterations(counter, perThreadIterations);
endLatch.countDown();
}
});
thread.start();
}
startBarrier.await();
long startNanos = System.nanoTime();
endLatch.await();
long elapsedNanos = System.nanoTime() startNanos;
assert counter.getValue() == iterations + (threadCount * 10 * warmUpIterations);
System.out.printf("%40s: %,d ms%n", name, TimeUnit.NANOSECONDS.toMillis(elapsedNanos));
}
private static void runIterations(Counter c, int n) {
for (int j = 0; j < n; j++) {
c.increment();
}
}
private interface Counter {
void increment();
long getValue();
}
private static class CounterBare implements Counter {
private long value = 0;
@Override
public void increment() {
value++;
}
@Override
public long getValue() {
return value;
}
}
private static class CounterWithVolatile implements Counter {
private volatile long value = 0;
@SuppressWarnings("NonAtomicOperationOnVolatileField")
@Override
public void increment() {
value++;
}
@Override
public long getValue() {
return value;
}
}
private static class CounterWithCAS implements Counter {
private AtomicLong value = new AtomicLong(0);
@Override
public void increment() {
value.incrementAndGet();
}
@Override
public long getValue() {
return value.get();
}
}
private static class CounterWithSynchronized implements Counter {
private final Object lock = new Object();
private long value = 0;
@Override
public void increment() {
synchronized (lock) {
value++;
}
}
@Override
public long getValue() {
return value;
}
}
private static class CounterWithLock implements Counter {
private final Lock lock = new ReentrantLock();
private long value = 0;
@Override
public void increment() {
lock.lock();
try {
value++;
} finally {
lock.unlock();
}
}
@Override
public long getValue() {
return value;
}
}
}

view raw
TestIncrement.java
hosted with ❤ by GitHub

The results I observed (running on Java 9 with a 2017 MacBook Pro with a 2.9 GHz 7th Generation Kaby Lake Intel Core i7 processor) are comparable to those reported by Martin 7 years ago.

Method Time (ms)
Kaby Lake, Java 10
Time (ms)
Westmere
Single thread 70 300
Single thread with volatile 2,700 4,700
Single thread with CAS 3,500 5,700
Single thread with synchronized 2,000
Single thread with lock 9,300 10,000
Two threads with CAS 10,800 18,000
Two threads with synchronized 22,400
Two threads with lock 52,500 118,000

While this micro-benchmark is not representative of real-world workloads (as explained here), tempted by its simplicity I plan to use it as the first benchmark to track optimizations to the air-java concurrency library. This would be followed up by a more comprehensive benchmark like this one, which measure both latency  and throughput under various configurations, and finally a real-world application.

Categories
Gradle Java Kotlin Software

Gradle Build with Java 9 Modules and Kotlin

When starting a new Java project recently, I found it surprisingly difficult to setup the Gradle build with support for Java 9 modules and the Kotlin language.

For others who might find themselves in the same bind, here is a gist with the simplest, minimal gradle setup I came up with that includes:

  • A multi-project gradle build,
  • Java 9 modules support,
  • IntelliJ IDEA integration,
  • Kotlin language modules with support for cross-references between Java and Kotlin code in the same module.
plugins {
id 'idea'
id 'com.zyxist.chainsaw' version '0.3.1' apply false
id "org.jetbrains.kotlin.jvm" version "1.2.31" apply false
}
subprojects {
apply plugin: 'idea'
apply plugin: 'java-library'
apply plugin: 'com.zyxist.chainsaw'
}
// The idea plugin derives the JDK name as '1.9' while IDEA uses '9' as the default name for JDK 9
idea.project.jdkName = '9'

view raw
build.gradle
hosted with ❤ by GitHub

apply plugin: 'org.jetbrains.kotlin.jvm'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
}
// Enable Kotlin compilation to Java 8 class files wiht method parameter name metadata
compileKotlin.kotlinOptions.jvmTarget = '1.8'
compileKotlin.kotlinOptions.javaParameters = true
// As per https://stackoverflow.com/a/47669720
// See also https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/9
compileKotlin.destinationDir = compileJava.destinationDir
jar.duplicatesStrategy = "exclude"
// Allow cross-references between java and kotlin
sourceSets.main.kotlin.srcDirs += 'src/main/java'

view raw
kotlin.gradle
hosted with ❤ by GitHub

Here is an proof-of-concept example of the above build scripts in action: https://github.com/nikolaybotevb/gradle-java9-kotlin.