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