The repository to demonstrate the impact of false sharing on performance.
Two classes were compared. One class was affected by false sharing,
the other one uses @Contended
annotation in order to solve the issue.
Class affected by false sharing:
Benchmark Mode Cnt Score Error Units
FalseSharingBenchmark.falseSharing avgt 20 48,897 ± 0,763 ns/op
FalseSharingBenchmark.falseSharing:incrementCounterA avgt 20 48,905 ± 0,898 ns/op
FalseSharingBenchmark.falseSharing:incrementCounterB avgt 20 48,889 ± 0,859 ns/op
After @Contended
was added to the class:
Benchmark Mode Cnt Score Error Units
FalseSharingBenchmark.contended avgt 20 6,765 ± 0,063 ns/op
FalseSharingBenchmark.contended:contendedIncrementA avgt 20 6,765 ± 0,063 ns/op
FalseSharingBenchmark.contended:contendedIncrementB avgt 20 6,765 ± 0,063 ns/op
mvn clean package
docker build -t false-sharing .
You can run benchmarks by yourself. Docker container shows benchmark results and perf
output.
Perf
is a Linux profiler and it's able to show the number of CPU cache load events.
Benchmark class affected by false-sharing:
docker run --cap-add SYS_ADMIN \
false-sharing \
java -jar -XX:-RestrictContended /opt/app/app.jar \
FalseSharingBenchmark
Benchmark class with @Contended
annotation:
docker run --cap-add SYS_ADMIN \
false-sharing \
java -jar -XX:-RestrictContended /opt/app/app.jar \
ContendedBenchmark
Run all benchmarks:
docker run --cap-add SYS_ADMIN \
false-sharing \
java -jar -XX:-RestrictContended /opt/app/app.jar