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