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