1 /*
   2  * Copyright (c) 2014, 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  * @test
  26  * @key gc
  27  * @bug 8049831
  28  * @library /testlibrary /testlibrary/whitebox
  29  * @build TestCMSClassUnloadingDisabledHWM
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  * @run driver TestCMSClassUnloadingDisabledHWM
  32  * @summary Test that -XX:-CMSClassUnloadingEnabled will trigger a Full GC when more than MetaspaceSize metadata is allocated.
  33  */
  34 
  35 import com.oracle.java.testlibrary.OutputAnalyzer;
  36 import com.oracle.java.testlibrary.ProcessTools;
  37 import sun.hotspot.WhiteBox;
  38 
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 
  42 public class TestCMSClassUnloadingDisabledHWM {
  43 
  44   private static OutputAnalyzer run(long metaspaceSize, long youngGenSize) throws Exception {
  45       ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  46         "-Xbootclasspath/a:.",
  47         "-XX:+WhiteBoxAPI",
  48         "-XX:MetaspaceSize=" + metaspaceSize,
  49         "-Xmn" + youngGenSize,
  50         "-XX:+UseConcMarkSweepGC",
  51         "-XX:-CMSClassUnloadingEnabled",
  52         "-XX:+PrintHeapAtGC",
  53         "-XX:+PrintGCDetails",
  54         "AllocateBeyondMetaspaceSize",
  55         "" + metaspaceSize,
  56         "" + youngGenSize);
  57     return new OutputAnalyzer(pb.start());
  58   }
  59 
  60   public static void main(String args[]) throws Exception {
  61     long metaspaceSize = 32 * 1024 * 1024;
  62     long youngGenSize = 32 * 1024 * 1024;
  63 
  64     OutputAnalyzer out = run(metaspaceSize, youngGenSize);
  65 
  66     // -XX:-CMSClassUnloadingEnabled is used, so we expect a full GC instead of a concurrent cycle.
  67     out.shouldMatch(".*Full GC.*");
  68     out.shouldNotMatch(".*CMS Initial Mark.*");
  69   }
  70 }
  71 
  72 class AllocateBeyondMetaspaceSize {
  73   public static Object dummy;
  74 
  75   public static void main(String [] args) {
  76     if (args.length != 2) {
  77       throw new IllegalArgumentException("Usage: <MetaspaceSize> <YoungGenSize>");
  78     }
  79 
  80     long metaspaceSize = Long.parseLong(args[0]);
  81     long youngGenSize = Long.parseLong(args[1]);
  82 
  83     run(metaspaceSize, youngGenSize);
  84   }
  85 
  86   private static void run(long metaspaceSize, long youngGenSize) {
  87     WhiteBox wb = WhiteBox.getWhiteBox();
  88 
  89     long allocationBeyondMetaspaceSize  = metaspaceSize * 2;
  90     long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);
  91 
  92     triggerYoungGC(youngGenSize);
  93 
  94     wb.freeMetaspace(null, metaspace, metaspace);
  95   }
  96 
  97   private static void triggerYoungGC(long youngGenSize) {
  98     long approxAllocSize = 32 * 1024;
  99     long numAllocations  = 2 * youngGenSize / approxAllocSize;
 100 
 101     for (long i = 0; i < numAllocations; i++) {
 102       dummy = new byte[(int)approxAllocSize];
 103     }
 104   }
 105 }