8028595: WhiteBox API for stress testing of TieredCompilation
authoriignatyev
Tue, 16 Dec 2014 17:56:24 +0300
changeset 28199 b7fb20a745f7
parent 28198 1e41fbf783e3
child 28200 38d3bf236c31
8028595: WhiteBox API for stress testing of TieredCompilation Reviewed-by: kvn
hotspot/src/share/vm/prims/whitebox.cpp
hotspot/test/compiler/whitebox/DeoptimizeFramesTest.java
--- a/hotspot/src/share/vm/prims/whitebox.cpp	Tue Dec 16 02:14:11 2014 +0300
+++ b/hotspot/src/share/vm/prims/whitebox.cpp	Tue Dec 16 17:56:24 2014 +0300
@@ -69,6 +69,14 @@
 bool WhiteBox::_used = false;
 volatile bool WhiteBox::compilation_locked = false;
 
+class VM_WhiteBoxOperation : public VM_Operation {
+ public:
+  VM_WhiteBoxOperation()                         { }
+  VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
+  bool allow_nested_vm_operations() const        { return true; }
+};
+
+
 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
   return (jlong)(void*)JNIHandles::resolve(obj);
 WB_END
@@ -404,6 +412,43 @@
   return env->FromReflectedMethod(method);
 }
 
+// Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
+class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
+ private:
+  int _result;
+  const bool _make_not_entrant;
+ public:
+  VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
+        _result(0), _make_not_entrant(make_not_entrant) { }
+  int  result() const { return _result; }
+
+  void doit() {
+    for (JavaThread* t = Threads::first(); t != NULL; t = t->next()) {
+      if (t->has_last_Java_frame()) {
+        for (StackFrameStream fst(t, UseBiasedLocking); !fst.is_done(); fst.next()) {
+          frame* f = fst.current();
+          if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
+            RegisterMap* reg_map = fst.register_map();
+            Deoptimization::deoptimize(t, *f, reg_map);
+            if (_make_not_entrant) {
+                nmethod* nm = CodeCache::find_nmethod(f->pc());
+                assert(nm != NULL, "sanity check");
+                nm->make_not_entrant();
+            }
+            ++_result;
+          }
+        }
+      }
+    }
+  }
+};
+
+WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
+  VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
+  VMThread::execute(&op);
+  return op.result();
+WB_END
+
 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
   MutexLockerEx mu(Compile_lock);
   CodeCache::mark_all_nmethods_for_deoptimization();
@@ -526,13 +571,6 @@
   return (mh->queued_for_compilation() || nm != NULL);
 WB_END
 
-class VM_WhiteBoxOperation : public VM_Operation {
- public:
-  VM_WhiteBoxOperation()                         { }
-  VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
-  bool allow_nested_vm_operations() const        { return true; }
-};
-
 class AlwaysFalseClosure : public BoolObjectClosure {
  public:
   bool do_object_b(oop p) { return false; }
@@ -761,7 +799,6 @@
   }
 WB_END
 
-
 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
   WhiteBox::compilation_locked = true;
 WB_END
@@ -1201,6 +1238,7 @@
   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
 #endif // INCLUDE_NMT
+  {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
   {CC"deoptimizeMethod",   CC"(Ljava/lang/reflect/Executable;Z)I",
                                                       (void*)&WB_DeoptimizeMethod  },
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/compiler/whitebox/DeoptimizeFramesTest.java	Tue Dec 16 17:56:24 2014 +0300
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test DeoptimizeFramesTest
+ * @bug 8028595
+ * @library /testlibrary /../../test/lib
+ * @build DeoptimizeFramesTest
+ * @run main ClassFileInstaller sun.hotspot.WhiteBox
+ *                              sun.hotspot.WhiteBox$WhiteBoxPermission
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
+ *                   -XX:+WhiteBoxAPI -Xmixed
+ *                   -XX:CompileCommand=compileonly,DeoptimizeFramesTest$TestCaseImpl::method
+ *                   -XX:-DeoptimizeRandom DeoptimizeFramesTest true
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
+ *                   -XX:+WhiteBoxAPI -Xmixed
+ *                   -XX:CompileCommand=compileonly,DeoptimizeFramesTest$TestCaseImpl::method
+ *                   -XX:-DeoptimizeRandom DeoptimizeFramesTest false
+ * @summary testing of WB::deoptimizeFrames()
+ */
+import java.lang.reflect.Executable;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Phaser;
+
+import sun.hotspot.code.NMethod;
+import com.oracle.java.testlibrary.Asserts;
+import com.oracle.java.testlibrary.InfiniteLoop;
+
+public class DeoptimizeFramesTest extends CompilerWhiteBoxTest {
+    private final boolean makeNotEntrant;
+    private final Phaser phaser;
+
+    private DeoptimizeFramesTest(boolean makeNotEntrant, Phaser phaser) {
+        super(new TestCaseImpl(phaser));
+        // to prevent inlining of #method
+        WHITE_BOX.testSetDontInlineMethod(method, true);
+        this.makeNotEntrant = makeNotEntrant;
+        this.phaser = phaser;
+        System.out.printf("DeoptimizeFramesTest(makeNotEntrant = %b)%n",
+                makeNotEntrant);
+    }
+
+    public static void main(String[] args) throws Exception {
+        Asserts.assertEQ(args.length, 1,
+                "[TESTBUG] args should contain 1 element");
+        new DeoptimizeFramesTest(Boolean.valueOf(args[0]), new Phaser()).runTest();
+    }
+
+    @Override
+    protected void test() throws Exception {
+        compile();
+        checkCompiled();
+        NMethod nm = NMethod.get(method, testCase.isOsr());
+
+        WHITE_BOX.deoptimizeFrames(makeNotEntrant);
+        // #method should still be compiled, since it didn't have frames on stack
+        checkCompiled();
+        NMethod nm2 = NMethod.get(method, testCase.isOsr());
+        Asserts.assertEQ(nm.compile_id, nm2.compile_id,
+                "should be the same nmethod");
+
+        phaser.register();
+        Thread t = new Thread(() -> compile(1));
+        t.start();
+        // pass 1st phase, #method is on stack
+        int p = phaser.arriveAndAwaitAdvance();
+        WHITE_BOX.deoptimizeFrames(makeNotEntrant);
+        // pass 2nd phase, #method can exit
+        phaser.awaitAdvance(phaser.arriveAndDeregister());
+
+        try {
+            t.join();
+        } catch (InterruptedException e) {
+            throw new Error("method '" + method + "' is still executing", e);
+        }
+
+        // invoke one more time to recompile not entrant if any
+        compile(1);
+
+        nm2 = NMethod.get(method, testCase.isOsr());
+        if (makeNotEntrant) {
+            if (nm2 != null) {
+                Asserts.assertNE(nm.compile_id, nm2.compile_id,
+                        String.format("compilation %d can't be available", nm.compile_id));
+            }
+        } else {
+            Asserts.assertEQ(nm.compile_id, nm2.compile_id, "should be the same nmethod");
+        }
+    }
+
+
+    private static class TestCaseImpl implements TestCase {
+        private static final Executable EXECUTABLE;
+        static {
+            try {
+                EXECUTABLE = TestCaseImpl.class.getDeclaredMethod("method");
+            } catch (NoSuchMethodException e) {
+                throw new Error("[TESTBUG] method not found", e);
+            }
+        }
+
+        private final Phaser phaser;
+
+        public TestCaseImpl(Phaser phaser) {
+            this.phaser = phaser;
+            phaser.register();
+        }
+
+        @Override
+        public String name() {
+            return "2phases";
+        }
+
+        @Override
+        public Executable getExecutable() {
+            return EXECUTABLE;
+        }
+
+        @Override
+        public Callable<Integer> getCallable() {
+            return () -> {
+                return method();
+            };
+        }
+
+        @Override
+        public boolean isOsr() {
+            return false;
+        }
+
+        private int method() {
+            phaser.arriveAndAwaitAdvance();
+            phaser.arriveAndAwaitAdvance();
+            return 0;
+        }
+    }
+}