Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/TestMultipleClasses.java
41153 views
/*1* Copyright (c) 2019, 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* @bug 813955126* @summary Scalability problem with redefinition - multiple code cache walks27* @requires vm.jvmti28* @library /test/lib29* @modules java.base/jdk.internal.misc30* @modules java.compiler31* java.instrument32* jdk.jartool/sun.tools.jar33* @run main RedefineClassHelper34* @run main/othervm/timeout=180 -javaagent:redefineagent.jar -XX:CompileThreshold=100 -Xlog:redefine+class+nmethod=debug TestMultipleClasses35*/3637import java.lang.instrument.*;38import java.lang.reflect.*;39import jdk.test.lib.compiler.InMemoryJavaCompiler;4041public class TestMultipleClasses extends ClassLoader {4243public static String B(int count) {44return new String("public class B" + count + " {" +45" public static void compiledMethod() { " +46" try{" +47" Thread.sleep(1); " +48" } catch(InterruptedException ie) {" +49" }" +50" }" +51"}");52}5354static String newB(int count) {55return new String("public class B" + count + " {" +56" public static void compiledMethod() { " +57" System.out.println(\"compiledMethod called " + count + "\");" +58" }" +59"}");60}6162static int index = 0;6364@Override65protected Class<?> findClass(String name) throws ClassNotFoundException {66if (name.equals("B" + index)) {67byte[] b = InMemoryJavaCompiler.compile(name, B(index));68return defineClass(name, b, 0, b.length);69} else {70return super.findClass(name);71}72}7374static void runCompiledMethodMethods(Class c, int count) throws Exception {75// Run for a while so they compile.76Object o = c.newInstance();77Method m = c.getMethod("compiledMethod");78for (int i = 0; i < count; i++) {79m.invoke(o);80}81}8283public static void main(String[] args) throws Exception {8485final int numberOfClasses = 20;86Class[] classes = new Class[numberOfClasses];87byte[][] newClass = new byte[numberOfClasses][];88ClassDefinition[] defs = new ClassDefinition[numberOfClasses];8990TestMultipleClasses loader = new TestMultipleClasses();9192// Load and start all the classes.93for (index = 0; index < numberOfClasses; index++) {94String name = new String("B" + index);95Class c = loader.findClass(name);9697runCompiledMethodMethods(c, 500);98// Make class definition for redefinition99classes[index] = c;100newClass[index] = InMemoryJavaCompiler.compile(c.getName(), newB(index));101defs[index] = new ClassDefinition(c, newClass[index]);102}103104long startTime = System.currentTimeMillis();105106// Redefine all classes.107RedefineClassHelper.instrumentation.redefineClasses(defs);108109long endTime = System.currentTimeMillis();110111System.out.println("Redefinition took " + (endTime - startTime) + " milliseconds");112113System.gc();114115// Run all new classes.116for (index = 0; index < numberOfClasses; index++) {117runCompiledMethodMethods(classes[index], 1);118}119}120}121122123