< prev index next >

test/hotspot/jtreg/serviceability/dcmd/vm/ClassLoaderStatsTest.java

Print this page




  44 import java.io.FileInputStream;
  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();
  88         while (lines.hasNext()) {
  89             String line = lines.next();
  90             Matcher m = clLine.matcher(line);
  91             if (m.matches()) {
  92                 // verify that DummyClassLoader has loaded 1 class, 1 anonymous class, and 1 hidden class
  93                 if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) {
  94                     System.out.println("DummyClassLoader line: " + line);
  95                     if (!m.group(1).equals("1")) {
  96                         Assert.fail("Should have loaded 1 class: " + line);
  97                     }
  98                     checkPositiveInt(m.group(2));
  99                     checkPositiveInt(m.group(3));
 100 
 101                     String next = lines.next();
 102                     System.out.println("DummyClassLoader next: " + next);
 103                     if (!next.contains("unsafe anonymous classes")) {
 104                         Assert.fail("Should have an anonymous class");
 105                     }
 106                     Matcher m1 = anonLine.matcher(next);
 107                     m1.matches();
 108                     if (!m1.group(1).equals("1")) {
 109                         Assert.fail("Should have loaded 1 anonymous class, but found : " + m1.group(1));
 110                     }
 111                     checkPositiveInt(m1.group(2));
 112                     checkPositiveInt(m1.group(3));
 113 
 114                     next = lines.next();
 115                     System.out.println("DummyClassLoader next: " + next);
 116                     if (!next.contains("hidden classes")) {
 117                         Assert.fail("Should have a hidden class");
 118                     }
 119                     Matcher m2 = anonLine.matcher(next);
 120                     m2.matches();
 121                     if (!m2.group(1).equals("1")) {
 122                         Assert.fail("Should have loaded 1 hidden class, but found : " + m2.group(1));

 123                     }
 124                     checkPositiveInt(m2.group(2));
 125                     checkPositiveInt(m2.group(3));
 126                 }
 127             }
 128         }
 129     }
 130 
 131     private static void checkPositiveInt(String s) {
 132         if (Integer.parseInt(s) <= 0) {
 133             Assert.fail("Value should have been > 0: " + s);
 134         }
 135     }
 136 
 137     public static class DummyClassLoader extends ClassLoader {
 138 
 139         public static final String CLASS_NAME = "TestClass";
 140 
 141         static ByteBuffer readClassFile(String name)
 142         {




  44 import java.io.FileInputStream;
  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     //                                                                  2      2048      1088   + hidden classes

  65     // 0x0000000000000000  0x0000000000000000  0x00007f00e852d190    1607   4628480   3931216  <boot class loader>
  66     //                                                                 38    124928     85856   + hidden classes
  67     // 0x00000008003b5508  0x0000000000000000  0x00007f001c2d4760       1      6144      4040  jdk.internal.reflect.DelegatingClassLoader
  68     // 0x000000080037f468  0x000000080037ee80  0x00007f00e868e3f0     228   1368064   1286672  jdk.internal.loader.ClassLoaders$AppClassLoader
  69     // ...
  70 
  71     static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");
  72     static Pattern hiddenLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");
  73 
  74     public static DummyClassLoader dummyloader;
  75 
  76     public void run(CommandExecutor executor) throws ClassNotFoundException {
  77 
  78         // create a classloader and load our special classes
  79         dummyloader = new DummyClassLoader();
  80         Class<?> c = Class.forName("TestClass", true, dummyloader);
  81         if (c.getClassLoader() != dummyloader) {
  82             Assert.fail("TestClass defined by wrong classloader: " + c.getClassLoader());
  83         }
  84 
  85         OutputAnalyzer output = executor.execute("VM.classloader_stats");
  86         Iterator<String> lines = output.asLines().iterator();
  87         while (lines.hasNext()) {
  88             String line = lines.next();
  89             Matcher m = clLine.matcher(line);
  90             if (m.matches()) {
  91                 // verify that DummyClassLoader has loaded 1 regular class and 2 hidden classes
  92                 if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) {
  93                     System.out.println("DummyClassLoader line: " + line);
  94                     if (!m.group(1).equals("1")) {
  95                         Assert.fail("Should have loaded 1 class: " + line);
  96                     }
  97                     checkPositiveInt(m.group(2));
  98                     checkPositiveInt(m.group(3));
  99 
 100                     String next = lines.next();
 101                     System.out.println("DummyClassLoader next: " + next);













 102                     if (!next.contains("hidden classes")) {
 103                         Assert.fail("Should have a hidden class");
 104                     }
 105                     Matcher m2 = hiddenLine.matcher(next);
 106                     m2.matches();
 107                     if (!m2.group(1).equals("2")) {
 108                         // anonymous classes are included in the hidden classes count.
 109                         Assert.fail("Should have loaded 2 hidden classes, but found : " + m2.group(1));
 110                     }
 111                     checkPositiveInt(m2.group(2));
 112                     checkPositiveInt(m2.group(3));
 113                 }
 114             }
 115         }
 116     }
 117 
 118     private static void checkPositiveInt(String s) {
 119         if (Integer.parseInt(s) <= 0) {
 120             Assert.fail("Value should have been > 0: " + s);
 121         }
 122     }
 123 
 124     public static class DummyClassLoader extends ClassLoader {
 125 
 126         public static final String CLASS_NAME = "TestClass";
 127 
 128         static ByteBuffer readClassFile(String name)
 129         {


< prev index next >