Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorInfo/GetOwnedMonitorInfoWithEATest.java
41155 views
/*1* Copyright (c) 2019 SAP SE. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 823067726* @summary Test JVMTI's GetOwnedMonitorInfo with scalar replaced objects and eliminated locks on stack (optimizations based on escape analysis).27* @comment Without RFE 8227745 escape analysis needs to be switched off to pass the test. For the implementation of RFE 8227745 it serves as a regression test.28* @requires (vm.compMode != "Xcomp" & vm.compiler2.enabled)29* @requires vm.jvmti30* @library /test/lib31* @compile GetOwnedMonitorInfoWithEATest.java32* @run main/othervm/native33* -agentlib:GetOwnedMonitorInfoWithEATest34* -XX:+UnlockDiagnosticVMOptions35* -Xms128m -Xmx128m36* -XX:CompileCommand=dontinline,*::dontinline_*37* -XX:+PrintCompilation38* -XX:+PrintInlining39* -XX:-TieredCompilation40* -Xbatch41* -XX:CICompilerCount=142* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking43* GetOwnedMonitorInfoWithEATest44* @run main/othervm/native45* -agentlib:GetOwnedMonitorInfoWithEATest46* -XX:+UnlockDiagnosticVMOptions47* -Xms128m -Xmx128m48* -XX:CompileCommand=dontinline,*::dontinline_*49* -XX:+PrintCompilation50* -XX:+PrintInlining51* -XX:-TieredCompilation52* -Xbatch53* -XX:CICompilerCount=154* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:-EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking -XX:-UseOptoBiasInlining55* GetOwnedMonitorInfoWithEATest56* @run main/othervm/native57* -agentlib:GetOwnedMonitorInfoWithEATest58* -XX:+UnlockDiagnosticVMOptions59* -Xms128m -Xmx128m60* -XX:CompileCommand=dontinline,*::dontinline_*61* -XX:+PrintCompilation62* -XX:+PrintInlining63* -XX:-TieredCompilation64* -Xbatch65* -XX:CICompilerCount=166* -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking67* GetOwnedMonitorInfoWithEATest68* @run main/othervm/native69* -agentlib:GetOwnedMonitorInfoWithEATest70* -XX:+UnlockDiagnosticVMOptions71* -Xms128m -Xmx128m72* -XX:CompileCommand=dontinline,*::dontinline_*73* -XX:+PrintCompilation74* -XX:+PrintInlining75* -XX:-TieredCompilation76* -Xbatch77* -XX:CICompilerCount=178* -XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:+UseBiasedLocking79* GetOwnedMonitorInfoWithEATest80* @run main/othervm/native81* -agentlib:GetOwnedMonitorInfoWithEATest82* -XX:+UnlockDiagnosticVMOptions83* -Xms128m -Xmx128m84* -XX:CompileCommand=dontinline,*::dontinline_*85* -XX:+PrintCompilation86* -XX:+PrintInlining87* -XX:-TieredCompilation88* -Xbatch89* -XX:CICompilerCount=190* -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking91* GetOwnedMonitorInfoWithEATest92* @run main/othervm/native93* -agentlib:GetOwnedMonitorInfoWithEATest94* -XX:+UnlockDiagnosticVMOptions95* -Xms128m -Xmx128m96* -XX:CompileCommand=dontinline,*::dontinline_*97* -XX:+PrintCompilation98* -XX:+PrintInlining99* -XX:-TieredCompilation100* -Xbatch101* -XX:CICompilerCount=1102* -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking103* GetOwnedMonitorInfoWithEATest104* @run main/othervm/native105* -agentlib:GetOwnedMonitorInfoWithEATest106* -XX:+UnlockDiagnosticVMOptions107* -Xms128m -Xmx128m108* -XX:CompileCommand=dontinline,*::dontinline_*109* -XX:+PrintCompilation110* -XX:+PrintInlining111* -XX:-TieredCompilation112* -Xbatch113* -XX:CICompilerCount=1114* -XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:+EliminateLocks -XX:+EliminateNestedLocks -XX:-UseBiasedLocking115* GetOwnedMonitorInfoWithEATest116*/117118import jdk.test.lib.Asserts;119120public class GetOwnedMonitorInfoWithEATest {121122public static final int COMPILE_THRESHOLD = 20000;123124/**125* Native wrapper arround JVMTI's GetOwnedMonitorInfo().126* @param t The thread for which the owned monitors information should be retrieved.127* @param ownedMonitors Array filled in by the call with the objects associated128* with the monitors owned by the given thread.129* @param depths Per owned monitor the depth of the frame were it was locked.130* Filled in by the call131* @return Number of monitors owned by the given thread.132*/133public static native int getOwnedMonitorInfo(Thread t, Object[] ownedMonitors);134135public static void main(String[] args) throws Exception {136new GetOwnedMonitorInfoWithEATest().runTest();137}138139public void runTest() throws Exception {140new TestCase_1().run();141new TestCase_2().run();142}143144public static abstract class TestCaseBase implements Runnable {145146public long checkSum;147public boolean doLoop;148public volatile long loopCount;149public volatile boolean targetIsInLoop;150151public void run() {152try {153msgHL("Executing test case " + getClass().getName());154warmUp();155runTest();156} catch (Exception e) {157Asserts.fail("Unexpected Exception", e);158}159}160161public void warmUp() {162int callCount = COMPILE_THRESHOLD + 1000;163doLoop = true;164while (callCount-- > 0) {165dontinline_testMethod();166}167}168169public abstract void runTest() throws Exception;170public abstract void dontinline_testMethod();171172public long dontinline_endlessLoop() {173long cs = checkSum;174while (doLoop && loopCount-- > 0) {175targetIsInLoop = true;176checkSum += checkSum % ++cs;177}178loopCount = 3;179targetIsInLoop = false;180return checkSum;181}182183public void waitUntilTargetThreadHasEnteredEndlessLoop() throws Exception {184while(!targetIsInLoop) {185msg("Target has not yet entered the loop. Sleep 200ms.");186try { Thread.sleep(200); } catch (InterruptedException e) { /*ignore */ }187}188msg("Target has entered the loop.");189}190191public void terminateEndlessLoop() throws Exception {192msg("Terminate endless loop");193do {194doLoop = false;195} while(targetIsInLoop);196}197198public void msg(String m) {199System.out.println();200System.out.println("### " + m);201System.out.println();202}203204public void msgHL(String m) {205System.out.println();206System.out.println("#####################################################");207System.out.println("### " + m);208System.out.println("###");209System.out.println();210}211}212213/**214* Starts target thread T and then queries monitor information for T using JVMTI's GetOwnedMonitorInfo().215* With escape analysis enabled the jit compiled method {@link #dontinline_testMethod()} has216* scalar replaced objects with eliminated (nested) locking in scope when the monitor217* information is retrieved. Effectively the objects escape through the JVMTI call. This works218* only with RFE 8227745. Without it escape analysis needs to be disabled.219*/220public static class TestCase_1 extends TestCaseBase {221222public void runTest() throws Exception {223loopCount = 1L << 62; // endless loop224Thread t1 = new Thread(() -> dontinline_testMethod(), "Target Thread");225t1.start();226try {227waitUntilTargetThreadHasEnteredEndlessLoop();228int expectedMonitorCount = 1;229int resultSize = expectedMonitorCount + 3;230Object[] ownedMonitors = new Object[resultSize];231msg("Get monitor info");232int monitorCount = getOwnedMonitorInfo(t1, ownedMonitors);233terminateEndlessLoop();234t1.join();235Asserts.assertGreaterThanOrEqual(monitorCount, 0, "getOwnedMonitorsFor() call failed");236msg("Monitor info:");237for (int i = 0; i < monitorCount; i++) {238System.out.println(i + ": cls=" + (ownedMonitors[i] != null ? ownedMonitors[i].getClass() : null));239}240Asserts.assertEQ(monitorCount, expectedMonitorCount, "unexpected monitor count returned by getOwnedMonitorsFor()");241Asserts.assertNotNull(ownedMonitors[0]);242Asserts.assertSame(ownedMonitors[0].getClass(), LockCls.class);243} finally {244terminateEndlessLoop();245t1.join();246}247}248249public void dontinline_testMethod() {250LockCls l1 = new LockCls(); // to be scalar replaced251synchronized (l1) {252inlinedTestMethodWithNestedLocking(l1);253}254}255256public void inlinedTestMethodWithNestedLocking(LockCls l1) {257synchronized (l1) { // nested258dontinline_endlessLoop();259}260}261}262263/**264* Similar to {@link TestCase_1}. Additionally the target thread T has got eliminated locking265* for a synchronized method of a different type {@linkplain LockCls2}.266*/267public static class TestCase_2 extends TestCaseBase {268269public void runTest() throws Exception {270loopCount = 1L << 62; // endless loop271Thread t1 = new Thread(() -> dontinline_testMethod(), "Target Thread");272t1.start();273try {274waitUntilTargetThreadHasEnteredEndlessLoop();275int expectedMonitorCount = 2;276int resultSize = expectedMonitorCount + 3;277Object[] ownedMonitors = new Object[resultSize];278msg("Get monitor info");279int monitorCount = getOwnedMonitorInfo(t1, ownedMonitors);280terminateEndlessLoop();281t1.join();282Asserts.assertGreaterThanOrEqual(monitorCount, 0, "getOwnedMonitorsFor() call failed");283msg("Monitor info:");284for (int i = 0; i < monitorCount; i++) {285System.out.println(i + ": cls=" + (ownedMonitors[i] != null ? ownedMonitors[i].getClass() : null));286}287Asserts.assertEQ(monitorCount, expectedMonitorCount, "unexpected monitor count returned by getOwnedMonitorsFor()");288Asserts.assertNotNull(ownedMonitors[0]);289Asserts.assertSame(ownedMonitors[0].getClass(), LockCls2.class);290291Asserts.assertNotNull(ownedMonitors[1]);292Asserts.assertSame(ownedMonitors[1].getClass(), LockCls.class);293} finally {294terminateEndlessLoop();295t1.join();296}297}298299public void dontinline_testMethod() {300LockCls l1 = new LockCls();301synchronized (l1) {302inlinedTestMethodWithNestedLocking(l1);303}304}305306public void inlinedTestMethodWithNestedLocking(LockCls l1) {307synchronized (l1) {308dontinline_testMethod2();309}310}311312public void dontinline_testMethod2() {313// Call synchronized method. Receiver of the call will be scalar replaced,314// and locking will be eliminated. Here we use a different type.315new LockCls2().inline_synchronized_testMethod(this);316}317}318319public static class LockCls {320}321322public static class LockCls2 {323public synchronized void inline_synchronized_testMethod(TestCaseBase testCase) {324testCase.dontinline_endlessLoop();325}326}327}328329330