1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8139551
  27  * @summary Scalability problem with redefinition - multiple code cache walks
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  * @modules java.compiler
  31  *          java.instrument
  32  *          jdk.jartool/sun.tools.jar
  33  * @run main RedefineClassHelper
  34  * @run main/othervm/timeout=180 -javaagent:redefineagent.jar -XX:CompileThreshold=100 -Xlog:redefine+class+nmethod=debug TestMultipleClasses
  35  */
  36 
  37 import java.lang.instrument.*;
  38 import java.lang.reflect.*;
  39 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  40 
  41 public class TestMultipleClasses extends ClassLoader {
  42 
  43     public static String B(int count) {
  44         return 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     }
  53 
  54     static String newB(int count) {
  55         return new String("public class B" + count + " {" +
  56                 "   public static void compiledMethod() { " +
  57                 "       System.out.println(\"compiledMethod called " + count + "\");" +
  58                 "   }" +
  59                 "}");
  60     }
  61 
  62     static int index = 0;
  63 
  64     @Override
  65     protected Class<?> findClass(String name) throws ClassNotFoundException {
  66         if (name.equals("B" + index)) {
  67             byte[] b = InMemoryJavaCompiler.compile(name, B(index));
  68             return defineClass(name, b, 0, b.length);
  69         } else {
  70             return super.findClass(name);
  71         }
  72     }
  73 
  74     static void runCompiledMethodMethods(Class c, int count) throws Exception {
  75         // Run for a while so they compile.
  76         Object o = c.newInstance();
  77         Method m = c.getMethod("compiledMethod");
  78         for (int i = 0; i < count; i++) {
  79             m.invoke(o);
  80         }
  81     }
  82 
  83     public static void main(String[] args) throws Exception {
  84 
  85         final int numberOfClasses = 20;
  86         Class[] classes = new Class[numberOfClasses];
  87         byte[][] newClass = new byte[numberOfClasses][];
  88         ClassDefinition[] defs = new ClassDefinition[numberOfClasses];
  89 
  90         TestMultipleClasses loader = new TestMultipleClasses();
  91 
  92         // Load and start all the classes.
  93         for (index = 0; index < numberOfClasses; index++) {
  94             String name = new String("B" + index);
  95             Class c = loader.findClass(name);
  96 
  97             runCompiledMethodMethods(c, 500);
  98             // Make class definition for redefinition
  99             classes[index] = c;
 100             newClass[index] = InMemoryJavaCompiler.compile(c.getName(), newB(index));
 101             defs[index] = new ClassDefinition(c, newClass[index]);
 102         }
 103 
 104         long startTime = System.currentTimeMillis();
 105 
 106         // Redefine all classes.
 107         RedefineClassHelper.instrumentation.redefineClasses(defs);
 108 
 109         long endTime = System.currentTimeMillis();
 110 
 111         System.out.println("Redefinition took " + (endTime - startTime) + " milliseconds");
 112 
 113         System.gc();
 114 
 115         // Run all new classes.
 116         for (index = 0; index < numberOfClasses; index++) {
 117             runCompiledMethodMethods(classes[index], 1);
 118         }
 119     }
 120 }