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.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  * @key gc
  43  * @library /test/lib
  44  * @modules java.base/jdk.internal.misc
  45  *          java.management
  46  * @requires vm.gc != "Epsilon"
  47  *
  48  * @compile TestMetaSpaceLog.java
  49  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  50  * @run driver gc.logging.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();
  63   }
  64 
  65   private static void verifyContainsMetaSpaceUpdate(OutputAnalyzer output) {
  66     // At least one metaspace line from GC should show GC being collected.
  67     boolean foundCollectedMetaSpace = output.asLines().stream()
  68         .filter(s -> s.contains("[gc,metaspace"))
  69         .anyMatch(TestMetaSpaceLog::check);
  70     Asserts.assertTrue(foundCollectedMetaSpace);
  71   }
  72 
  73   private static boolean check(String line) {
  74     Matcher m = metaSpaceRegexp.matcher(line);
  75     Asserts.assertTrue(m.matches(), "Unexpected line for metaspace logging: " + line);
  76     long before = Long.parseLong(m.group(1));
  77     long after  = Long.parseLong(m.group(2));
  78     return before > after;
  79   }
  80 
  81   private static void testMetaSpaceUpdate() throws Exception {
  82     // Propagate test.src for the jar file.
  83     String testSrc= "-Dtest.src=" + System.getProperty("test.src", ".");
  84 
  85     ProcessBuilder pb =
  86       ProcessTools.createJavaProcessBuilder(
  87           true,
  88           "-Xlog:gc*",
  89           "-Xbootclasspath/a:.",
  90           "-XX:+UnlockDiagnosticVMOptions",
  91           "-XX:+WhiteBoxAPI",
  92           "-Xmx1000M",
  93           "-Xms1000M",
  94           testSrc, StressMetaSpace.class.getName());
  95 
  96     OutputAnalyzer output = null;
  97     try {
  98       output = new OutputAnalyzer(pb.start());
  99       verifyContainsMetaSpaceUpdate(output);
 100     } catch (Exception e) {
 101       // For error diagnosis: print and throw.
 102       e.printStackTrace();
 103       output.reportDiagnosticSummary();
 104       throw e;
 105     }
 106   }
 107 
 108   static class StressMetaSpace {
 109     private static URL[] urls = new URL[1];
 110 
 111     static {
 112       try {
 113         File jarFile = new File(System.getProperty("test.src") + "/testcases.jar");
 114         urls[0] = jarFile.toURI().toURL();
 115       } catch (Exception e) {
 116         e.printStackTrace();
 117       }
 118     }
 119 
 120     public static void main(String args[]) {
 121       WhiteBox wb = WhiteBox.getWhiteBox();
 122       for(int i = 0; i < 10000; i++) {
 123         loadClass(wb);
 124       }
 125       wb.fullGC();
 126     }
 127 
 128     public static void loadClass(WhiteBox wb) {
 129       try {
 130         URLClassLoader ucl = new URLClassLoader(urls);
 131         Class.forName("case00", false, ucl);
 132       } catch (Exception e) {
 133         e.printStackTrace();
 134       }
 135     }
 136   }
 137 }