1 /* 2 * Copyright (c) 2014, 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 * @library /testlibrary 27 * @build com.oracle.java.testlibrary.* 28 * @build com.oracle.java.testlibrary.dcmd.* 29 * @run testng ClassLoaderStatsTest 30 */ 31 32 import com.oracle.java.testlibrary.OutputAnalyzer; 33 import com.oracle.java.testlibrary.dcmd.CommandExecutor; 34 import com.oracle.java.testlibrary.dcmd.JMXExecutor; 35 import org.testng.annotations.Test; 36 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.IOException; 40 import java.nio.ByteBuffer; 41 import java.nio.channels.FileChannel; 42 import java.util.Iterator; 43 import java.util.regex.Matcher; 44 import java.util.regex.Pattern; 45 46 public class ClassLoaderStatsTest { 47 48 // ClassLoader Parent CLD* Classes ChunkSz BlockSz Type 49 // 0x00000007c0215928 0x0000000000000000 0x0000000000000000 0 0 0 org.eclipse.osgi.baseadaptor.BaseAdaptor$1 50 // 0x00000007c0009868 0x0000000000000000 0x00007fc52aebcc80 1 6144 3768 sun.reflect.DelegatingClassLoader 51 // 0x00000007c0009868 0x0000000000000000 0x00007fc52b8916d0 1 6144 3688 sun.reflect.DelegatingClassLoader 52 // 0x00000007c0009868 0x00000007c0038ba8 0x00007fc52afb8760 1 6144 3688 sun.reflect.DelegatingClassLoader 53 // 0x00000007c0009868 0x0000000000000000 0x00007fc52afbb1a0 1 6144 3688 sun.reflect.DelegatingClassLoader 54 // 0x0000000000000000 0x0000000000000000 0x00007fc523416070 5019 30060544 29956216 <boot classloader> 55 // 455 1210368 672848 + unsafe anonymous classes 56 // 0x00000007c016b5c8 0x00000007c0038ba8 0x00007fc52a995000 5 8192 5864 org.netbeans.StandardModule$OneModuleClassLoader 57 // 0x00000007c0009868 0x00000007c016b5c8 0x00007fc52ac13640 1 6144 3896 sun.reflect.DelegatingClassLoader 58 // ... 59 60 static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)"); 61 static Pattern anonLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*"); 62 63 public static DummyClassLoader dummyloader; 64 65 public void run(CommandExecutor executor) throws ClassNotFoundException { 66 67 // create a classloader and load our special class 68 dummyloader = new DummyClassLoader(); 69 Class<?> c = Class.forName("TestClass", true, dummyloader); 70 if (c.getClassLoader() != dummyloader) { 71 throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader()); 72 } 73 74 OutputAnalyzer output = executor.execute("VM.classloader_stats"); 75 Iterator<String> lines = output.asLines().iterator(); 76 while (lines.hasNext()) { 77 String line = lines.next(); 78 Matcher m = clLine.matcher(line); 79 if (m.matches()) { 80 // verify that DummyClassLoader has loaded 1 class and 1 anonymous class 81 if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) { 82 System.out.println("line: " + line); 83 if (!m.group(1).equals("1")) { 84 throw new RuntimeException("Should have loaded 1 class: " + line); 85 } 86 checkPositiveInt(m.group(2)); 87 checkPositiveInt(m.group(3)); 88 89 String next = lines.next(); 90 System.out.println("next: " + next); 91 Matcher m1 = anonLine.matcher(next); 92 m1.matches(); 93 if (!m1.group(1).equals("1")) { 94 throw new RuntimeException("Should have loaded 1 anonymous class, but found : " + m1.group(1)); 95 } 96 checkPositiveInt(m1.group(2)); 97 checkPositiveInt(m1.group(3)); 98 } 99 } 100 } 101 } 102 103 private static void checkPositiveInt(String s) { 104 if (Integer.parseInt(s) <= 0) { 105 throw new RuntimeException("Value should have been > 0: " + s); 106 } 107 } 108 109 public static class DummyClassLoader extends ClassLoader { 110 111 public static final String CLASS_NAME = "TestClass"; 112 113 static ByteBuffer readClassFile(String name) 114 { 115 File f = new File(System.getProperty("test.classes", "."), 116 name); 117 try (FileInputStream fin = new FileInputStream(f); 118 FileChannel fc = fin.getChannel()) 119 { 120 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 121 } catch (IOException e) { 122 throw new RuntimeException("Can't open file: " + name, e); 123 } 124 } 125 126 protected Class<?> loadClass(String name, boolean resolve) 127 throws ClassNotFoundException 128 { 129 Class<?> c; 130 if (!"TestClass".equals(name)) { 131 c = super.loadClass(name, resolve); 132 } else { 133 // should not delegate to the system class loader 134 c = findClass(name); 135 if (resolve) { 136 resolveClass(c); 137 } 138 } 139 return c; 140 } 141 142 protected Class<?> findClass(String name) 143 throws ClassNotFoundException 144 { 145 if (!"TestClass".equals(name)) { 146 throw new ClassNotFoundException("Unexpected class: " + name); 147 } 148 return defineClass(name, readClassFile(name + ".class"), null); 149 } 150 } /* DummyClassLoader */ 151 152 @Test 153 public void jmx() throws ClassNotFoundException { 154 run(new JMXExecutor()); 155 } 156 } 157 158 class TestClass { 159 static { 160 // force creation of anonymous class (for the lambdaform) 161 Runnable r = () -> System.out.println("Hello"); 162 r.run(); 163 } 164 }