1 /*
  2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2018, Google and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 package gc.logging;
 26 
 27 import java.io.File;
 28 import java.net.URL;
 29 import java.net.URLClassLoader;
 30 import java.util.function.Predicate;
 31 import java.util.regex.Pattern;
 32 import java.util.regex.Matcher;
 33 
 34 import jdk.test.lib.Asserts;
 35 import jdk.test.lib.process.OutputAnalyzer;
 36 import jdk.test.lib.process.ProcessTools;
 37 import sun.hotspot.WhiteBox;
 38 
 39 /*
 40  * @test TestMetaSpaceLog
 41  * @bug 8211123
 42  * @summary Ensure that the Metaspace is updated in the log
 43  * @requires vm.gc=="null"
 44  * @key gc
 45  * @library /test/lib
 46  * @modules java.base/jdk.internal.misc
 47  *          java.management
 48  *
 49  * @compile TestMetaSpaceLog.java
 50  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 51  * @run main gc.logging.TestMetaSpaceLog
 52  */
 53 
 54 public class TestMetaSpaceLog {
 55   private static Pattern metaSpaceRegexp;
 56 
 57   static {
 58     // Do this once here.
 59     metaSpaceRegexp = Pattern.compile(".*Metaspace: ([0-9]+).*->([0-9]+).*");
 60   }
 61 
 62   public static void main(String[] args) throws Exception {
 63     testMetaSpaceUpdate("UseParallelGC");
 64     testMetaSpaceUpdate("UseG1GC");
 65     testMetaSpaceUpdate("UseConcMarkSweepGC");
 66     testMetaSpaceUpdate("UseSerialGC");
 67   }
 68 
 69   private static void verifyContainsMetaSpaceUpdate(OutputAnalyzer output) {
 70     Predicate<String> collectedMetaSpace = line -> check(line);
 71 
 72     // At least one metaspace line from GC should show GC being collected.
 73     boolean foundCollectedMetaSpace = output.asLines().stream()
 74         .filter(s -> s.contains("[gc,metaspace"))
 75         .anyMatch(TestMetaSpaceLog::check);
 76     Asserts.assertTrue(foundCollectedMetaSpace);
 77   }
 78 
 79   private static boolean check(String line) {
 80     Matcher m = metaSpaceRegexp.matcher(line);
 81     Asserts.assertTrue(m.matches(), "Unexpected line for metaspace logging: " + line);
 82     long before = Long.parseLong(m.group(1));
 83     long after  = Long.parseLong(m.group(2));
 84     return before > after;
 85   }
 86 
 87   private static void testMetaSpaceUpdate(String gcFlag) throws Exception {
 88     // Propagate test.src for the jar file.
 89     String testSrc= "-Dtest.src=" + System.getProperty("test.src", ".");
 90 
 91     System.err.println("Testing with GC Flag: " + gcFlag);
 92     ProcessBuilder pb =
 93       ProcessTools.createJavaProcessBuilder(
 94           "-XX:+" + gcFlag,
 95           "-Xlog:gc*",
 96           "-Xbootclasspath/a:.",
 97           "-XX:+UnlockDiagnosticVMOptions",
 98           "-XX:+WhiteBoxAPI",
 99           "-Xmx1000M",
100           "-Xms1000M",
101           testSrc, StressMetaSpace.class.getName());
102 
103     OutputAnalyzer output = null;
104     try {
105       output = new OutputAnalyzer(pb.start());
106       verifyContainsMetaSpaceUpdate(output);
107     } catch (Exception e) {
108       // For error diagnosis: print and throw.
109       e.printStackTrace();
110       output.reportDiagnosticSummary();
111       throw e;
112     }
113   }
114 
115   static class StressMetaSpace {
116     private static URL[] urls = new URL[1];
117 
118     static {
119       try {
120         File jarFile = new File(System.getProperty("test.src") + "/testcases.jar");
121         urls[0] = jarFile.toURI().toURL();
122       } catch (Exception e) {
123         e.printStackTrace();
124       }
125     }
126 
127     public static void main(String args[]) {
128       WhiteBox wb = WhiteBox.getWhiteBox();
129       for(int i = 0; i < 10000; i++) {
130         loadClass(wb);
131       }
132       wb.fullGC();
133     }
134 
135     public static void loadClass(WhiteBox wb) {
136       try {
137         URLClassLoader ucl = new URLClassLoader(urls);
138         Class.forName("case00", false, ucl);
139       } catch (Exception e) {
140         e.printStackTrace();
141       }
142     }
143   }
144 }