Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/deoptimization/test08/test08.java
40948 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. 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*26* @summary converted from VM Testbase jit/deoptimization/test08.27* VM Testbase keywords: [jit, quick]28*29* @library /vmTestbase30* /test/lib31* @run main/othervm jit.deoptimization.test08.test0832*/3334package jit.deoptimization.test08;3536import nsk.share.TestFailure;3738/*39* Simple recursion that should causes hotspot to deoptimize40* used_alot and A.foo and A.bar methods41* Expected result for the test is: 042*43* run with the -XX:TraceDeoptimization to observ the result.44*/4546public class test08 {47public static void main (String[] args) {48A obj = new A();49for (int index = 0; index < 1; index++) {50obj.used_alot();51}52}53}5455class A {56static int result = 0;5758synchronized public int foo(int index, int iter) {59synchronized (A.a_ext) {60if (index == 0)61return result;62else if (iter <= sIteration / 2) {63result = bar(index) * foo(--index, iter);64} else {65try {66// halfway through the max iterations67// create a B. This should cause the68// used_alot to be deoptimized69// Call b.frame for the remaining part of the recursion70if (b == null)71b = Class.forName("jit.deoptimization.test08.B").newInstance();72result *= ((B)b).foo(index, iter);73} catch (Exception x) {74throw new TestFailure("Class not found: B");75}76}77}78return result;79}8081// Does nothing but it will let be over written in class C, See Below82synchronized public int bar(int index) {83synchronized (a_ext) {84for (int i=0; i<5; i++)85index--;86}87return 0;88}8990synchronized public void used_alot() {91int result = 1;9293synchronized (a_ext) {94for (int index = 1; index <= sIteration; index++) {95result *= foo(index, index);96}97}9899if (result != 0) {100System.err.println ("Result: " + result);101System.exit (1);102}103}104105protected Object b = null;106protected static final int sIteration = 1000;107protected static final Object a_ext = new Object();108}109110class B extends A {111// Override foo in order to force deoptimization.112// Also creates and instance of class C in the last113// iteration in order to force A.foo to get deoptimized114// otherwise just do something stupid.115synchronized public int foo(int index, int iter) {116synchronized (A.a_ext) {117synchronized (B.b_ext) {118// Make sure that this class was created at least119// halfway through the iteration. Simple sanity check120if (iter < sIteration /2) {121System.out.println ("class B create to early");122System.exit(1);123}124125if (iter == sIteration) {126try {127result = ((C)(Class.forName("jit.deoptimization.test08.C").newInstance())).foo(index,iter);128result *= ((C)(Class.forName("jit.deoptimization.test08.C").newInstance())).bar(index);129130} catch (Exception x) {131throw new TestFailure("Class not found: C");132}133} else {134result = bar(index);135if (index != 0)136result += foo(--index, iter);137}138}139}140return result;141}142protected static final Object b_ext = new Object();143}144145class C extends B {146synchronized public int foo(int index, int iter) {147synchronized (A.a_ext) {148synchronized (B.b_ext) {149synchronized (C.c_ext) {150for (int i=0; i<iter; i++) {151result = bar(i);152if (i == iter-2) {153try {154result = ((D)(Class.forName("jit.deoptimization.test08.D").newInstance())).bar(index);155} catch (Exception x) {156throw new TestFailure("Class not found: D");157}158}159}160}161}162}163return result;164}165protected static final Object c_ext = new Object();166}167168class D extends C {169synchronized public int bar(int index) {170return 1;171}172}173174175