Wed, 21 May 2014 10:56:41 -0700
Added tag jdk8u20-b15 for changeset 8c785f9bde6f
coleenp@1853 | 1 | /* |
trims@1907 | 2 | * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. |
coleenp@1853 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
coleenp@1853 | 4 | * |
coleenp@1853 | 5 | * This code is free software; you can redistribute it and/or modify it |
coleenp@1853 | 6 | * under the terms of the GNU General Public License version 2 only, as |
coleenp@1853 | 7 | * published by the Free Software Foundation. |
coleenp@1853 | 8 | * |
coleenp@1853 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
coleenp@1853 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
coleenp@1853 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
coleenp@1853 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
coleenp@1853 | 13 | * accompanied this code). |
coleenp@1853 | 14 | * |
coleenp@1853 | 15 | * You should have received a copy of the GNU General Public License version |
coleenp@1853 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
coleenp@1853 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
coleenp@1853 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
coleenp@1853 | 22 | * |
coleenp@1853 | 23 | */ |
coleenp@1853 | 24 | |
coleenp@1853 | 25 | import java.io.ByteArrayOutputStream; |
coleenp@1853 | 26 | import java.io.IOException; |
coleenp@1853 | 27 | import java.io.OutputStream; |
coleenp@1853 | 28 | import java.io.PrintWriter; |
coleenp@1853 | 29 | import java.io.StringWriter; |
coleenp@1853 | 30 | |
coleenp@1853 | 31 | import java.lang.reflect.Method; |
coleenp@1853 | 32 | import java.net.URI; |
coleenp@1853 | 33 | import java.util.Arrays; |
coleenp@1853 | 34 | import java.util.Vector; |
coleenp@1853 | 35 | |
coleenp@1853 | 36 | import javax.tools.Diagnostic; |
coleenp@1853 | 37 | import javax.tools.DiagnosticCollector; |
coleenp@1853 | 38 | import javax.tools.FileObject; |
coleenp@1853 | 39 | import javax.tools.ForwardingJavaFileManager; |
coleenp@1853 | 40 | import javax.tools.JavaCompiler; |
coleenp@1853 | 41 | import javax.tools.JavaCompiler.CompilationTask; |
coleenp@1853 | 42 | import javax.tools.JavaFileManager; |
coleenp@1853 | 43 | import javax.tools.JavaFileObject; |
coleenp@1853 | 44 | import javax.tools.JavaFileObject.Kind; |
coleenp@1853 | 45 | import javax.tools.SimpleJavaFileObject; |
coleenp@1853 | 46 | import javax.tools.StandardJavaFileManager; |
coleenp@1853 | 47 | import javax.tools.ToolProvider; |
coleenp@1853 | 48 | |
coleenp@1853 | 49 | /* |
coleenp@1853 | 50 | * @test SortMethodsTest |
coleenp@1853 | 51 | * @bug 6925573 |
coleenp@1853 | 52 | * @summary verify that class loading does not need quadratic time with regard to the number of class |
coleenp@1853 | 53 | methods. |
coleenp@1853 | 54 | * @run main SortMethodsTest |
coleenp@1853 | 55 | * @author volker.simonis@gmail.com |
coleenp@1853 | 56 | */ |
coleenp@1853 | 57 | |
coleenp@1853 | 58 | public class SortMethodsTest { |
coleenp@1853 | 59 | |
coleenp@1853 | 60 | static String createClass(String name, int nrOfMethods) { |
coleenp@1853 | 61 | StringWriter sw = new StringWriter(); |
coleenp@1853 | 62 | PrintWriter pw = new PrintWriter(sw); |
coleenp@1853 | 63 | pw.println("public class " + name + "{"); |
coleenp@1853 | 64 | for (int i = 0; i < nrOfMethods; i++) { |
coleenp@1853 | 65 | pw.println(" public void m" + i + "() {}"); |
coleenp@1853 | 66 | } |
coleenp@1853 | 67 | pw.println(" public static String sayHello() {"); |
coleenp@1853 | 68 | pw.println(" return \"Hello from class \" + " + name + |
coleenp@1853 | 69 | ".class.getName() + \" with \" + " + name + |
coleenp@1853 | 70 | ".class.getDeclaredMethods().length + \" methods\";"); |
coleenp@1853 | 71 | pw.println(" }"); |
coleenp@1853 | 72 | pw.println("}"); |
coleenp@1853 | 73 | pw.close(); |
coleenp@1853 | 74 | return sw.toString(); |
coleenp@1853 | 75 | } |
coleenp@1853 | 76 | |
coleenp@1853 | 77 | public static void main(String args[]) { |
coleenp@1853 | 78 | |
coleenp@1853 | 79 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
coleenp@1853 | 80 | DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>(); |
coleenp@1853 | 81 | final String cName = new String("ManyMethodsClass"); |
coleenp@1853 | 82 | Vector<Long> results = new Vector<Long>(); |
coleenp@1853 | 83 | |
coleenp@1853 | 84 | for (int i = 6; i < 600000; i*=10) { |
coleenp@1853 | 85 | String klass = createClass(cName, i); |
coleenp@1853 | 86 | JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass); |
coleenp@1853 | 87 | MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file); |
coleenp@1853 | 88 | CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file)); |
coleenp@1853 | 89 | |
coleenp@1853 | 90 | if (task.call()) { |
coleenp@1853 | 91 | try { |
coleenp@1853 | 92 | MemoryClassLoader mcl = new MemoryClassLoader(file); |
coleenp@1853 | 93 | long start = System.nanoTime(); |
coleenp@1853 | 94 | Class<? extends Object> c = Class.forName(cName, true, mcl); |
coleenp@1853 | 95 | long end = System.nanoTime(); |
coleenp@1853 | 96 | results.add(end - start); |
coleenp@1853 | 97 | Method m = c.getDeclaredMethod("sayHello", new Class[0]); |
coleenp@1853 | 98 | String ret = (String)m.invoke(null, new Object[0]); |
coleenp@1853 | 99 | System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)"); |
coleenp@1853 | 100 | } catch (Exception e) { |
coleenp@1853 | 101 | System.err.println(e); |
coleenp@1853 | 102 | } |
coleenp@1853 | 103 | } |
coleenp@1853 | 104 | else { |
coleenp@1853 | 105 | System.out.println(klass); |
coleenp@1853 | 106 | System.out.println(); |
coleenp@1853 | 107 | for (Diagnostic diag : diags.getDiagnostics()) { |
coleenp@1853 | 108 | System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition()); |
coleenp@1853 | 109 | System.out.println(diag.getSource() + "\n" + diag.getMessage(null)); |
coleenp@1853 | 110 | } |
coleenp@1853 | 111 | } |
coleenp@1853 | 112 | } |
coleenp@1853 | 113 | |
coleenp@1853 | 114 | long lastRatio = 0; |
coleenp@1853 | 115 | for (int i = 2; i < results.size(); i++) { |
coleenp@1853 | 116 | long normalized1 = Math.max(results.get(i-1) - results.get(0), 1); |
coleenp@1853 | 117 | long normalized2 = Math.max(results.get(i) - results.get(0), 1); |
coleenp@1853 | 118 | long ratio = normalized2/normalized1; |
coleenp@1853 | 119 | lastRatio = ratio; |
coleenp@1853 | 120 | System.out.println("10 x more methods requires " + ratio + " x more time"); |
coleenp@1853 | 121 | } |
coleenp@1853 | 122 | // The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines |
coleenp@1853 | 123 | if (lastRatio > 80) { |
coleenp@1853 | 124 | throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!"); |
coleenp@1853 | 125 | } |
coleenp@1853 | 126 | } |
coleenp@1853 | 127 | } |
coleenp@1853 | 128 | |
coleenp@1853 | 129 | class JavaMemoryFileObject extends SimpleJavaFileObject { |
coleenp@1853 | 130 | |
coleenp@1853 | 131 | private final String code; |
coleenp@1853 | 132 | private ByteArrayOutputStream byteCode; |
coleenp@1853 | 133 | |
coleenp@1853 | 134 | JavaMemoryFileObject(String name, String code) { |
coleenp@1853 | 135 | super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE); |
coleenp@1853 | 136 | this.code = code; |
coleenp@1853 | 137 | } |
coleenp@1853 | 138 | |
coleenp@1853 | 139 | @Override |
coleenp@1853 | 140 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
coleenp@1853 | 141 | return code; |
coleenp@1853 | 142 | } |
coleenp@1853 | 143 | |
coleenp@1853 | 144 | @Override |
coleenp@1853 | 145 | public OutputStream openOutputStream() { |
coleenp@1853 | 146 | byteCode = new ByteArrayOutputStream(); |
coleenp@1853 | 147 | return byteCode; |
coleenp@1853 | 148 | } |
coleenp@1853 | 149 | |
coleenp@1853 | 150 | byte[] getByteCode() { |
coleenp@1853 | 151 | return byteCode.toByteArray(); |
coleenp@1853 | 152 | } |
coleenp@1853 | 153 | } |
coleenp@1853 | 154 | |
coleenp@1853 | 155 | class MemoryClassLoader extends ClassLoader { |
coleenp@1853 | 156 | |
coleenp@1853 | 157 | private final JavaMemoryFileObject jfo; |
coleenp@1853 | 158 | |
coleenp@1853 | 159 | public MemoryClassLoader(JavaMemoryFileObject jfo) { |
coleenp@1853 | 160 | this.jfo = jfo; |
coleenp@1853 | 161 | } |
coleenp@1853 | 162 | |
coleenp@1853 | 163 | public Class findClass(String name) { |
coleenp@1853 | 164 | byte[] b = jfo.getByteCode(); |
coleenp@1853 | 165 | return defineClass(name, b, 0, b.length); |
coleenp@1853 | 166 | } |
coleenp@1853 | 167 | } |
coleenp@1853 | 168 | |
coleenp@1853 | 169 | class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { |
coleenp@1853 | 170 | |
coleenp@1853 | 171 | private final JavaFileObject jfo; |
coleenp@1853 | 172 | |
coleenp@1853 | 173 | public MemoryFileManager(StandardJavaFileManager jfm, JavaFileObject jfo) { |
coleenp@1853 | 174 | super(jfm); |
coleenp@1853 | 175 | this.jfo = jfo; |
coleenp@1853 | 176 | } |
coleenp@1853 | 177 | |
coleenp@1853 | 178 | @Override |
coleenp@1853 | 179 | public FileObject getFileForInput(Location location, String packageName, |
coleenp@1853 | 180 | String relativeName) throws IOException { |
coleenp@1853 | 181 | return jfo; |
coleenp@1853 | 182 | } |
coleenp@1853 | 183 | |
coleenp@1853 | 184 | @Override |
coleenp@1853 | 185 | public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName, |
coleenp@1853 | 186 | Kind kind, FileObject outputFile) throws IOException { |
coleenp@1853 | 187 | return jfo; |
coleenp@1853 | 188 | } |
coleenp@1853 | 189 | |
coleenp@1853 | 190 | } |