changeset 240:127ca0c87a5c

Use AtomicXFieldUpdaters to cover the AtomicX values from false sharing.
author shade
date Tue, 12 Nov 2013 16:40:52 +0100
parents 238aad2065dc
children 8e1914f965b4
files jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java
diffstat 2 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Nov 12 16:17:08 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Tue Nov 12 16:40:52 2013 +0100
@@ -80,6 +80,7 @@
 import java.util.TreeMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 
 /**
  * @author staffan.friberg@oracle.com
@@ -397,6 +398,7 @@
     private void generateImport(PrintWriter writer) {
         writer.println("import " + List.class.getName() + ';');
         writer.println("import " + AtomicInteger.class.getName() + ';');
+        writer.println("import " + AtomicIntegerFieldUpdater.class.getName() + ';');
         writer.println("import " + Arrays.class.getName() + ';');
         writer.println("import " + TimeUnit.class.getName() + ';');
         writer.println("import " + Generated.class.getName() + ';');
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Tue Nov 12 16:17:08 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Tue Nov 12 16:40:52 2013 +0100
@@ -256,7 +256,7 @@
             if (!hasHelpers.contains(so)) continue;
 
             if (type == HelperType.SETUP) {
-                result.add("while(!" + so.localIdentifier + ".setup" + helperLevel + "Mutex.compareAndSet(0, 1)) {");
+                result.add("while(!" + so.type + ".setup" + helperLevel + "MutexUpdater.compareAndSet(" + so.localIdentifier + ", 0, 1)) {");
                 result.add("    if (Thread.interrupted()) throw new InterruptedException();");
                 result.add("}");
                 result.add("try {");
@@ -269,12 +269,12 @@
                 result.add("        " + so.localIdentifier + ".ready" + helperLevel + " = true;");
                 result.add("    }");
                 result.add("} finally {");
-                result.add("    " + so.localIdentifier + ".setup" + helperLevel + "Mutex.set(0);");
+                result.add("    " + so.type + ".setup" + helperLevel + "MutexUpdater.set(" + so.localIdentifier + ", 0);");
                 result.add("}");
             }
 
             if (type == HelperType.TEARDOWN) {
-                result.add("while(!" + so.localIdentifier + ".tear" + helperLevel + "Mutex.compareAndSet(0, 1)) {");
+                result.add("while(!" + so.type + ".tear" + helperLevel + "MutexUpdater.compareAndSet(" + so.localIdentifier + ", 0, 1)) {");
                 result.add("    if (Thread.interrupted()) throw new InterruptedException();");
                 result.add("}");
                 result.add("try {");
@@ -287,7 +287,7 @@
                 result.add("        " + so.localIdentifier + ".ready" + helperLevel + " = false;");
                 result.add("    }");
                 result.add("} finally {");
-                result.add("    " + so.localIdentifier + ".tear" + helperLevel + "Mutex.set(0);");
+                result.add("    " + so.type + ".tear" + helperLevel + "MutexUpdater.set(" + so.localIdentifier + ", 0);");
                 result.add("}");
             }
         }
@@ -421,9 +421,14 @@
             result.add("");
             result.add("static class " + so.type + "_B2 extends " + so.type + "_B1 {");
 
+
             for (Level level : Level.values()) {
-                result.add("    final AtomicInteger setup" + level + "Mutex = new AtomicInteger();");
-                result.add("    final AtomicInteger tear" + level + "Mutex = new AtomicInteger();");
+                result.add("    volatile int setup" + level + "Mutex;");
+                result.add("    volatile int tear" + level + "Mutex;");
+                result.add("    final static AtomicIntegerFieldUpdater setup" + level + "MutexUpdater = " +
+                        "AtomicIntegerFieldUpdater.newUpdater(" + so.type + "_B2.class, \"setup" + level + "Mutex\");");
+                result.add("    final static AtomicIntegerFieldUpdater tear" + level + "MutexUpdater = " +
+                        "AtomicIntegerFieldUpdater.newUpdater(" + so.type + "_B2.class, \"tear" + level + "Mutex\");");
                 result.add("");
             }