1 /*
   2  * Copyright (c) 2016, 2018, Red Hat, Inc. 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 /*
  26  * @test TestAllocHumongousFragment
  27  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
  28  * @key randomness
  29  * @requires vm.gc.Shenandoah
  30  * @library /test/lib
  31  *
  32  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  34  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
  35  *      TestAllocHumongousFragment
  36  *
  37  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  39  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
  40  *      TestAllocHumongousFragment
  41  *
  42  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  43  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  44  *      -XX:-ShenandoahDegeneratedGC
  45  *      TestAllocHumongousFragment
  46  *
  47  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  48  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  49  *      -XX:+ShenandoahDegeneratedGC
  50  *      TestAllocHumongousFragment
  51  */
  52 
  53 /*
  54  * @test TestAllocHumongousFragment
  55  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
  56  * @key randomness
  57  * @requires vm.gc.Shenandoah
  58  * @library /test/lib
  59  *
  60  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  61  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  62  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
  63  *      TestAllocHumongousFragment
  64  *
  65  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  66  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  67  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
  68  *      TestAllocHumongousFragment
  69  *
  70  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  72  *      -XX:+ShenandoahOOMDuringEvacALot
  73  *      TestAllocHumongousFragment
  74  *
  75  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  76  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  77  *      -XX:+ShenandoahAllocFailureALot
  78  *      TestAllocHumongousFragment
  79  *
  80  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  81  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  82  *      -XX:+ShenandoahVerify
  83  *      TestAllocHumongousFragment
  84  *
  85  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  86  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  87  *      TestAllocHumongousFragment
  88  *
  89  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  90  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
  91  *      TestAllocHumongousFragment
  92  *
  93  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
  94  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  95  *      TestAllocHumongousFragment
  96  */
  97 
  98 /*
  99  * @test TestAllocHumongousFragment
 100  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 101  * @key randomness
 102  * @requires vm.gc.Shenandoah
 103  * @library /test/lib
 104  *
 105  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 106  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 107  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
 108  *      TestAllocHumongousFragment
 109  *
 110  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 111  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 112  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
 113  *      TestAllocHumongousFragment
 114  *
 115  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 116  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 117  *      -XX:+ShenandoahOOMDuringEvacALot
 118  *      TestAllocHumongousFragment
 119  *
 120  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 121  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 122  *      -XX:+ShenandoahAllocFailureALot
 123  *      TestAllocHumongousFragment
 124  *
 125  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 126  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 127  *      -XX:+ShenandoahVerify
 128  *      TestAllocHumongousFragment
 129  *
 130  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
 131  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 132  *      TestAllocHumongousFragment
 133  */
 134 
 135 import java.util.*;
 136 import jdk.test.lib.Utils;
 137 
 138 public class TestAllocHumongousFragment {
 139 
 140     static final long TARGET_MB = Long.getLong("target", 30_000); // 30 Gb allocations
 141     static final long LIVE_MB   = Long.getLong("occupancy", 700); // 700 Mb alive
 142 
 143     static volatile Object sink;
 144 
 145     static List<int[]> objects;
 146 
 147     public static void main(String[] args) throws Exception {
 148         final int min = 128 * 1024;
 149         final int max = 16 * 1024 * 1024;
 150         final long count = TARGET_MB * 1024 * 1024 / (16 + 4 * (min + (max - min) / 2));
 151 
 152         objects = new ArrayList<>();
 153         long current = 0;
 154 
 155         Random rng = Utils.getRandomInstance();
 156         for (long c = 0; c < count; c++) {
 157             while (current > LIVE_MB * 1024 * 1024) {
 158                 int idx = rng.nextInt(objects.size());
 159                 int[] remove = objects.remove(idx);
 160                 current -= remove.length * 4 + 16;
 161             }
 162 
 163             int[] newObj = new int[min + rng.nextInt(max - min)];
 164             current += newObj.length * 4 + 16;
 165             objects.add(newObj);
 166             sink = new Object();
 167 
 168             System.out.println("Allocated: " + (current / 1024 / 1024) + " Mb");
 169         }
 170     }
 171 
 172 }