1 /*
   2  * Copyright (c) 2018, 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 import java.io.File;
  26 import java.net.URL;
  27 import java.net.URLClassLoader;
  28 import java.util.List;
  29 import java.util.function.Predicate;
  30 import java.util.regex.Pattern;
  31 import java.util.regex.Matcher;
  32 
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 import jdk.test.lib.process.ProcessTools;
  36 import sun.hotspot.WhiteBox;
  37 
  38 /*
  39  * @test TestMetaSpaceLog
  40  * @bug 8211123
  41  * @summary Ensure that the Metaspace is updated in the log
  42  * @requires vm.gc=="null"
  43  * @key gc
  44  * @library /test/lib
  45  * @modules java.base/jdk.internal.misc
  46  *          java.management
  47  *
  48  * @compile TestMetaSpaceLog.java
  49  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  50  * @run main TestMetaSpaceLog
  51  */
  52 
  53 public class TestMetaSpaceLog {
  54   private static Pattern metaSpaceRegexp;
  55 
  56   static {
  57     // Do this once here.
  58     metaSpaceRegexp = Pattern.compile(".*Metaspace: ([0-9]+).*->([0-9]+).*");
  59   }
  60 
  61   public static void main(String[] args) throws Exception {
  62     testMetaSpaceUpdate("UseParallelGC");
  63     testMetaSpaceUpdate("UseG1GC");
  64     testMetaSpaceUpdate("UseConcMarkSweepGC");
  65     testMetaSpaceUpdate("UseSerialGC");
  66   }
  67 
  68   private static void verifyContainsMetaSpaceUpdate(OutputAnalyzer output) {
  69     Predicate<String> collectedMetaSpace = line -> check(line);
  70 
  71     // At least one metaspace line from GC should show GC being collected.
  72     boolean foundCollectedMetaSpace = output.asLines().stream()
  73         .filter(s -> s.contains("[gc,metaspace"))
  74         .anyMatch(TestMetaSpaceLog::check);
  75     Asserts.assertTrue(foundCollectedMetaSpace);
  76   }
  77 
  78   private static boolean check(String line) {
  79     Matcher m = metaSpaceRegexp.matcher(line);
  80     Asserts.assertTrue(m.matches(), "Unexpected line for metaspace logging: " + line);
  81     long before = Long.parseLong(m.group(1));
  82     long after  = Long.parseLong(m.group(2));
  83     return before > after;
  84   }
  85 
  86   private static void testMetaSpaceUpdate(String gcFlag) throws Exception {
  87     // Propagate test.src for the jar file.
  88     String testSrc= "-Dtest.src=" + System.getProperty("test.src", ".");
  89 
  90     System.err.println("Testing with GC Flag: " + gcFlag);
  91     ProcessBuilder pb =
  92       ProcessTools.createJavaProcessBuilder(
  93           "-XX:+" + gcFlag,
  94           "-Xlog:gc*",
  95           "-Xbootclasspath/a:.",
  96           "-XX:+UnlockDiagnosticVMOptions",
  97           "-XX:+WhiteBoxAPI",
  98           "-Xmx1000M",
  99           "-Xms1000M",
 100           testSrc, StressMetaSpace.class.getName());
 101 
 102     OutputAnalyzer output = null;
 103     try {
 104       output = new OutputAnalyzer(pb.start());
 105       verifyContainsMetaSpaceUpdate(output);
 106     } catch (Exception e) {
 107       // For error diagnosis: print and throw.
 108       e.printStackTrace();
 109       output.reportDiagnosticSummary();
 110       throw e;
 111     }
 112   }
 113 
 114   static class StressMetaSpace {
 115     private static URL[] urls = new URL[1];
 116 
 117     static {
 118       try {
 119         File jarFile = new File(System.getProperty("test.src") + "/testcases.jar");
 120         urls[0] = jarFile.toURI().toURL();
 121       } catch (Exception e) {
 122         e.printStackTrace();
 123       }
 124     }
 125 
 126     public static void main(String args[]) {
 127       WhiteBox wb = WhiteBox.getWhiteBox();
 128       for(int i = 0; i < 10000; i++) {
 129         loadClass(wb);
 130       }
 131       wb.fullGC();
 132     }
 133 
 134     public static void loadClass(WhiteBox wb) {
 135       try {
 136         URLClassLoader ucl = new URLClassLoader(urls);
 137         Class.forName("case00", false, ucl);
 138       } catch (Exception e) {
 139         e.printStackTrace();
 140       }
 141     }
 142   }
 143 }