1 /*
   2  * Copyright (c) 2016, 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 /*
  26  * @test ClassLoadUnloadTest
  27  * @bug 8142506
  28  * @library /testlibrary
  29  * @run driver ClassLoadUnloadTest
  30  */
  31 
  32 import jdk.test.lib.*;
  33 import java.lang.ref.WeakReference;
  34 import java.lang.reflect.Method;
  35 
  36 public class ClassLoadUnloadTest {
  37     private static OutputAnalyzer out;
  38     private static ProcessBuilder pb;
  39     private static class ClassUnloadTestMain {
  40         
  41         static String klass = "public class Empty {"
  42                   + " public void bigLoop(){}"
  43                   + " public String toString() { return \"nothing\"; } }";
  44         
  45         public static volatile WeakReference<ByteCodeLoader> clweak;
  46         public static byte[] garbage;
  47         
  48         public static void main(String... args) throws Exception {
  49             ByteCodeLoader cl = new ByteCodeLoader("Empty", InMemoryJavaCompiler.compile("Empty", klass));
  50             Class<?> c = cl.loadClass("Empty");
  51             Object o = c.newInstance();
  52             Method m = c.getMethod("bigLoop");
  53             
  54             for ( int i=0; i<10001; i++){
  55                 m.invoke(o);
  56             }
  57             
  58             clweak = new WeakReference<>(cl);
  59             cl = null; c = null; o = null; m = null;
  60             while(clweak.get() != null) {
  61                   garbage = new byte[8192];
  62                   System.gc();
  63             }
  64         }
  65     }
  66 
  67     static void checkFor(String... outputStrings) throws Exception {
  68         out = new OutputAnalyzer(pb.start());
  69         for (String s: outputStrings) {
  70             out.shouldContain(s);
  71         }
  72         out.shouldHaveExitValue(0);
  73     }
  74 
  75     static void checkAbsent(String... outputStrings) throws Exception {
  76         out = new OutputAnalyzer(pb.start());
  77         for (String s: outputStrings) {
  78             out.shouldNotContain(s);
  79         }
  80         out.shouldHaveExitValue(0);
  81     }
  82 
  83     public static void main(String... args) throws Exception {
  84 
  85         // (1) -Xlog:classunload=info
  86         pb = ProcessTools.createJavaProcessBuilder(
  87             "-Xlog:classunload=info", "-Xmx64m", ClassUnloadTestMain.class.getName()
  88         );
  89         checkFor("[classunload]", "unloading class");  
  90 
  91         // (2) -Xlog:classunload=trace
  92         pb = ProcessTools.createJavaProcessBuilder(
  93             "-Xlog:classunload=trace", "-Xmx64m", ClassUnloadTestMain.class.getName()
  94         );
  95         checkFor("[classunload]", "making nmethod");
  96 
  97         // (3) -Xlog:classunload=off
  98         pb = ProcessTools.createJavaProcessBuilder(
  99             "-Xlog:classunload=off", "-Xmx64m", ClassUnloadTestMain.class.getName()
 100         );
 101         checkAbsent("[classunload]");
 102 
 103         // (4) -XX:+TraceClassUnloading 
 104         pb = ProcessTools.createJavaProcessBuilder(
 105             "-XX:+TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName()
 106         );
 107         checkFor("[classunload]", "unloading class");
 108 
 109         // (5) -XX:-TraceClassUnloading
 110         pb = ProcessTools.createJavaProcessBuilder(
 111             "-XX:-TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName()
 112         );
 113         checkAbsent("[classunload]");
 114 
 115         // (6) -Xlog:classload=info
 116         pb = ProcessTools.createJavaProcessBuilder(
 117             "-Xlog:classload=info", "-Xmx64m", ClassUnloadTestMain.class.getName()
 118         );
 119         checkFor("[classload]", "java.lang.Object", "source:");
 120 
 121         // (7) -Xlog:classload=debug
 122         pb = ProcessTools.createJavaProcessBuilder(
 123             "-Xlog:classload=debug", "-Xmx64m", ClassUnloadTestMain.class.getName()
 124         );
 125         checkFor("[classload]", "java.lang.Object", "source:", "klass:", "super:", "loader:", "bytes:");
 126 
 127         // (8) -Xlog:classload=off
 128         pb = ProcessTools.createJavaProcessBuilder(
 129             "-Xlog:classload=off", "-Xmx64m", ClassUnloadTestMain.class.getName()
 130         );
 131         checkAbsent("[classload]");
 132 
 133         // (9) -XX:+TraceClassLoading
 134         pb = ProcessTools.createJavaProcessBuilder(
 135             "-XX:+TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName()
 136         );
 137         checkFor("[classload]", "java.lang.Object", "source:");
 138 
 139         // (10) -XX:-TraceClassLoading
 140         pb = ProcessTools.createJavaProcessBuilder(
 141             "-XX:-TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName()
 142         );
 143         checkAbsent("[classload]");
 144 
 145         // (11) -verbose:class 
 146         pb = ProcessTools.createJavaProcessBuilder(
 147             "-verbose:class", "-Xmx64m", ClassUnloadTestMain.class.getName()
 148         );
 149         checkFor("[classload]", "java.lang.Object", "source:");
 150         checkFor("[classunload]", "unloading class");
 151          
 152         // (12) -Xlog:classloaderdata=trace
 153         pb = ProcessTools.createJavaProcessBuilder(
 154             "-Xlog:classloaderdata=trace", "-Xmx64m", ClassUnloadTestMain.class.getName()
 155         );
 156         checkFor("[classloaderdata]", "create class loader data");
 157   
 158     }
 159 }