1 /*
   2  * Copyright (c) 2012, 2020, 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 package gc.g1;
  25 
  26 /*
  27  * @test TestHumongousAllocConcurrentStart
  28  * @bug 7168848
  29  * @summary G1: humongous object allocations should initiate marking cycles when necessary
  30  * @requires vm.gc.G1
  31  * @library /test/lib
  32  * @modules java.base/jdk.internal.misc
  33  *          java.management
  34  * @run driver gc.g1.TestHumongousAllocConcurrentStart
  35  */
  36 
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 public class TestHumongousAllocConcurrentStart {
  41     // Heap sizes < 224 MB are increased to 224 MB if vm_page_size == 64K to
  42     // fulfill alignment constraints.
  43     private static final int heapSize                       = 224; // MB
  44     private static final int heapRegionSize                 = 1;   // MB
  45     private static final int initiatingHeapOccupancyPercent = 50;  // %
  46 
  47     public static void main(String[] args) throws Exception {
  48         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  49             "-XX:+UseG1GC",
  50             "-Xms" + heapSize + "m",
  51             "-Xmx" + heapSize + "m",
  52             "-XX:G1HeapRegionSize=" + heapRegionSize + "m",
  53             "-XX:InitiatingHeapOccupancyPercent=" + initiatingHeapOccupancyPercent,
  54             "-Xlog:gc",
  55             HumongousObjectAllocator.class.getName());
  56 
  57         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  58         output.shouldContain("Pause Young (Concurrent Start) (G1 Humongous Allocation)");
  59         output.shouldNotContain("Full GC");
  60         output.shouldHaveExitValue(0);
  61     }
  62 
  63     static class HumongousObjectAllocator {
  64         private static byte[] dummy;
  65 
  66         public static void main(String [] args) throws Exception {
  67             // Make object size 75% of region size
  68             final int humongousObjectSize =
  69                 (int)(heapRegionSize * 1024 * 1024 * 0.75);
  70 
  71             // Number of objects to allocate to go above IHOP
  72             final int humongousObjectAllocations =
  73                 (int)((heapSize * initiatingHeapOccupancyPercent / 100.0) / heapRegionSize) + 1;
  74 
  75             // Allocate
  76             for (int i = 1; i <= humongousObjectAllocations; i++) {
  77                 System.out.println("Allocating humongous object " + i + "/" + humongousObjectAllocations +
  78                                    " of size " + humongousObjectSize + " bytes");
  79                 dummy = new byte[humongousObjectSize];
  80             }
  81         }
  82     }
  83 }
  84