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