45 import java.io.IOException;
46 import java.lang.invoke.MethodHandles;
47 import java.lang.invoke.MethodHandles.Lookup;
48 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
49 import java.nio.ByteBuffer;
50 import java.nio.channels.FileChannel;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.util.Iterator;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56
57 import jdk.internal.misc.Unsafe;
58
59 public class ClassLoaderStatsTest {
60
61 // Expected output from VM.classloader_stats:
62 // ClassLoader Parent CLD* Classes ChunkSz BlockSz Type
63 // 0x0000000800bd3830 0x000000080037f468 0x00007f001c2ea170 1 10240 4672 ClassLoaderStatsTest$DummyClassLoader
64 // 1 2048 1080 + unsafe anonymous classes
65 // 1 2048 1088 + hidden weak classes
66 // 0x0000000000000000 0x0000000000000000 0x00007f00e852d190 1607 4628480 3931216 <boot class loader>
67 // 38 124928 85856 + hidden weak classes
68 // 0x00000008003b5508 0x0000000000000000 0x00007f001c2d4760 1 6144 4040 jdk.internal.reflect.DelegatingClassLoader
69 // 0x000000080037f468 0x000000080037ee80 0x00007f00e868e3f0 228 1368064 1286672 jdk.internal.loader.ClassLoaders$AppClassLoader
70 // ...
71
72 static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");
73 static Pattern anonLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");
74
75 public static DummyClassLoader dummyloader;
76
77 public void run(CommandExecutor executor) throws ClassNotFoundException {
78
79 // create a classloader and load our special classes
80 dummyloader = new DummyClassLoader();
81 Class<?> c = Class.forName("TestClass", true, dummyloader);
82 if (c.getClassLoader() != dummyloader) {
83 Assert.fail("TestClass defined by wrong classloader: " + c.getClassLoader());
84 }
85
86 OutputAnalyzer output = executor.execute("VM.classloader_stats");
87 Iterator<String> lines = output.asLines().iterator();
177 }
178 return defineClass(name, readClassFile(name + ".class"), null);
179 }
180 } /* DummyClassLoader */
181
182 @Test
183 public void jmx() throws ClassNotFoundException {
184 run(new JMXExecutor());
185 }
186 }
187
188 class HiddenClass { }
189
190 class TestClass {
191 private static final String HCName = "HiddenClass.class";
192 private static final String DIR = System.getProperty("test.classes");
193 static Unsafe unsafe = Unsafe.getUnsafe();
194
195 static {
196 try {
197 // Create a hidden weak class and an anonymous class.
198 byte[] klassBuf = readClassFile(DIR + File.separator + HCName);
199 Class<?> hc = defineHiddenClass(klassBuf);
200 Class ac = unsafe.defineAnonymousClass(TestClass.class, klassBuf, new Object[0]);
201 } catch (Throwable e) {
202 throw new RuntimeException("Unexpected exception in TestClass: " + e.getMessage());
203 }
204 }
205
206
207 static byte[] readClassFile(String classFileName) throws Exception {
208 File classFile = new File(classFileName);
209 try (FileInputStream in = new FileInputStream(classFile);
210 ByteArrayOutputStream out = new ByteArrayOutputStream())
211 {
212 int b;
213 while ((b = in.read()) != -1) {
214 out.write(b);
215 }
216 return out.toByteArray();
217 }
|
45 import java.io.IOException;
46 import java.lang.invoke.MethodHandles;
47 import java.lang.invoke.MethodHandles.Lookup;
48 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
49 import java.nio.ByteBuffer;
50 import java.nio.channels.FileChannel;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.util.Iterator;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56
57 import jdk.internal.misc.Unsafe;
58
59 public class ClassLoaderStatsTest {
60
61 // Expected output from VM.classloader_stats:
62 // ClassLoader Parent CLD* Classes ChunkSz BlockSz Type
63 // 0x0000000800bd3830 0x000000080037f468 0x00007f001c2ea170 1 10240 4672 ClassLoaderStatsTest$DummyClassLoader
64 // 1 2048 1080 + unsafe anonymous classes
65 // 1 2048 1088 + hidden classes
66 // 0x0000000000000000 0x0000000000000000 0x00007f00e852d190 1607 4628480 3931216 <boot class loader>
67 // 38 124928 85856 + hidden classes
68 // 0x00000008003b5508 0x0000000000000000 0x00007f001c2d4760 1 6144 4040 jdk.internal.reflect.DelegatingClassLoader
69 // 0x000000080037f468 0x000000080037ee80 0x00007f00e868e3f0 228 1368064 1286672 jdk.internal.loader.ClassLoaders$AppClassLoader
70 // ...
71
72 static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");
73 static Pattern anonLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");
74
75 public static DummyClassLoader dummyloader;
76
77 public void run(CommandExecutor executor) throws ClassNotFoundException {
78
79 // create a classloader and load our special classes
80 dummyloader = new DummyClassLoader();
81 Class<?> c = Class.forName("TestClass", true, dummyloader);
82 if (c.getClassLoader() != dummyloader) {
83 Assert.fail("TestClass defined by wrong classloader: " + c.getClassLoader());
84 }
85
86 OutputAnalyzer output = executor.execute("VM.classloader_stats");
87 Iterator<String> lines = output.asLines().iterator();
177 }
178 return defineClass(name, readClassFile(name + ".class"), null);
179 }
180 } /* DummyClassLoader */
181
182 @Test
183 public void jmx() throws ClassNotFoundException {
184 run(new JMXExecutor());
185 }
186 }
187
188 class HiddenClass { }
189
190 class TestClass {
191 private static final String HCName = "HiddenClass.class";
192 private static final String DIR = System.getProperty("test.classes");
193 static Unsafe unsafe = Unsafe.getUnsafe();
194
195 static {
196 try {
197 // Create a hidden non-strong class and an anonymous class.
198 byte[] klassBuf = readClassFile(DIR + File.separator + HCName);
199 Class<?> hc = defineHiddenClass(klassBuf);
200 Class ac = unsafe.defineAnonymousClass(TestClass.class, klassBuf, new Object[0]);
201 } catch (Throwable e) {
202 throw new RuntimeException("Unexpected exception in TestClass: " + e.getMessage());
203 }
204 }
205
206
207 static byte[] readClassFile(String classFileName) throws Exception {
208 File classFile = new File(classFileName);
209 try (FileInputStream in = new FileInputStream(classFile);
210 ByteArrayOutputStream out = new ByteArrayOutputStream())
211 {
212 int b;
213 while ((b = in.read()) != -1) {
214 out.write(b);
215 }
216 return out.toByteArray();
217 }
|