changeset 127:9e18c79cb757

More precise layout checks in both BlackHole and InfraControl. You can't be paranoid enough.
author shade
date Wed, 21 Aug 2013 13:38:01 +0400
parents d1accd77974e
children d1120166f46c
files jmh-core/src/main/java/org/openjdk/jmh/logic/BlackHole.java jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java
diffstat 2 files changed, 60 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/logic/BlackHole.java	Wed Aug 21 13:04:36 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/logic/BlackHole.java	Wed Aug 21 13:38:01 2013 +0400
@@ -34,14 +34,18 @@
     See the rationale for BlackHoleL1..BlackHoleL4 classes below.
  */
 
-class BlackholeL1 {
+class BlackHoleL0 {
+    public int markerBegin;
+}
+
+class BlackHoleL1 extends BlackHoleL0 {
     public int p01, p02, p03, p04, p05, p06, p07, p08;
     public int p11, p12, p13, p14, p15, p16, p17, p18;
     public int p21, p22, p23, p24, p25, p26, p27, p28;
     public int p31, p32, p33, p34, p35, p36, p37, p38;
 }
 
-class BlackHoleL2 extends BlackholeL1 {
+class BlackHoleL2 extends BlackHoleL1 {
     public volatile byte b1 = 1, b2 = 2;
     public volatile boolean bool1 = false, bool2 = true;
     public volatile char c1 = 'A', c2 = 'B';
@@ -65,7 +69,7 @@
 }
 
 class BlackHoleL4 extends BlackHoleL3 {
-    public int marker;
+    public int markerEnd;
 }
 
 /**
@@ -179,16 +183,17 @@
 
     static void check(String fieldName) {
         final long requiredGap = 128;
-        long markerOff = getOffset("marker");
+        long markerBegin = getOffset("markerBegin");
+        long markerEnd = getOffset("markerEnd");
         long off = getOffset(fieldName);
-        if (markerOff - off < requiredGap) {
-            throw new IllegalStateException("Consistency check failed for " + fieldName + ", off = " + off + ", markerOff = " + markerOff);
+        if (markerEnd - off < requiredGap || off - markerBegin < requiredGap) {
+            throw new IllegalStateException("Consistency check failed for " + fieldName + ", off = " + off + ", markerBegin = " + markerBegin + ", markerEnd = " + markerEnd);
         }
     }
 
     static long getOffset(String fieldName) {
         try {
-            Field f = BlackHoleL4.class.getField(fieldName);
+            Field f = BlackHole.class.getField(fieldName);
             return U.objectFieldOffset(f);
         } catch (NoSuchFieldException e) {
             throw new IllegalStateException(e);
--- a/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Wed Aug 21 13:04:36 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/logic/InfraControl.java	Wed Aug 21 13:38:01 2013 +0400
@@ -25,7 +25,9 @@
 package org.openjdk.jmh.logic;
 
 import org.openjdk.jmh.runner.parameters.TimeValue;
+import sun.misc.Unsafe;
 
+import java.lang.reflect.Field;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -41,6 +43,46 @@
  */
 public class InfraControl extends InfraControlL4 {
 
+    private static final Unsafe U;
+
+    static {
+        try {
+            Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
+            unsafe.setAccessible(true);
+            U = (Unsafe) unsafe.get(null);
+        } catch (NoSuchFieldException e) {
+            throw new IllegalStateException(e);
+        } catch (IllegalAccessException e) {
+            throw new IllegalStateException(e);
+        }
+
+        consistencyCheck();
+    }
+
+    static void consistencyCheck() {
+        // checking the fields are not reordered
+        check("isDone");
+    }
+
+    static void check(String fieldName) {
+        final long requiredGap = 128;
+        long markerBegin = getOffset("markerBegin");
+        long markerEnd = getOffset("markerEnd");
+        long off = getOffset(fieldName);
+        if (markerEnd - off < requiredGap || off - markerBegin < requiredGap) {
+            throw new IllegalStateException("Consistency check failed for " + fieldName + ", off = " + off + ", markerBegin = " + markerBegin + ", markerEnd = " + markerEnd);
+        }
+    }
+
+    static long getOffset(String fieldName) {
+        try {
+            Field f = InfraControl.class.getField(fieldName);
+            return U.objectFieldOffset(f);
+        } catch (NoSuchFieldException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
     public InfraControl(int threads, boolean syncIterations, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, TimeUnit timeUnit) {
         super(threads, syncIterations, loopTime, preSetup, preTearDown, lastIteration, timeUnit);
     }
@@ -95,7 +137,11 @@
     }
 }
 
-class InfraControlL1 {
+class InfraControlL0 {
+    public int markerBegin;
+}
+
+class InfraControlL1 extends InfraControlL0 {
     public int p01, p02, p03, p04, p05, p06, p07, p08;
     public int p11, p12, p13, p14, p15, p16, p17, p18;
     public int p21, p22, p23, p24, p25, p26, p27, p28;
@@ -177,7 +223,7 @@
 }
 
 class InfraControlL4 extends InfraControlL3 {
-    public int marker;
+    public int markerEnd;
 
     public InfraControlL4(int threads, boolean syncIterations, TimeValue loopTime, CountDownLatch preSetup, CountDownLatch preTearDown, boolean lastIteration, TimeUnit timeUnit) {
         super(threads, syncIterations, loopTime, preSetup, preTearDown, lastIteration, timeUnit);