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