1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2018, Red Hat, Inc. 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 
 26 /*
 27  * @test id=passive
 28  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 29  * @key randomness
 30  * @requires vm.gc.Shenandoah
 31  * @library /test/lib
 32  *
 33  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 34  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 35  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 36  *      TestAllocHumongousFragment
 37  *
 38  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 39  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 40  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 41  *      TestAllocHumongousFragment
 42  *
 43  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 44  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 45  *      -XX:-ShenandoahDegeneratedGC
 46  *      TestAllocHumongousFragment
 47  *
 48  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 49  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 50  *      -XX:+ShenandoahDegeneratedGC
 51  *      TestAllocHumongousFragment
 52  */
 53 
 54 /*
 55  * @test id=aggressive
 56  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 57  * @key randomness
 58  * @requires vm.gc.Shenandoah
 59  * @library /test/lib
 60  *
 61  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 62  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 63  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
 64  *      TestAllocHumongousFragment
 65  *
 66  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 67  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 68  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
 69  *      TestAllocHumongousFragment
 70  *
 71  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 72  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 73  *      -XX:+ShenandoahOOMDuringEvacALot
 74  *      TestAllocHumongousFragment
 75  *
 76  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 77  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 78  *      -XX:+ShenandoahAllocFailureALot
 79  *      TestAllocHumongousFragment
 80  */
 81 
 82 /*
 83  * @test id=adaptive
 84  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 85  * @key randomness
 86  * @requires vm.gc.Shenandoah
 87  * @library /test/lib
 88  *
 89  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 90  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 91  *      -XX:+ShenandoahVerify
 92  *      TestAllocHumongousFragment
 93  *
 94  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 95  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 96  *      TestAllocHumongousFragment
 97  */
 98 
 99 /*
100  * @test id=static
101  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
102  * @key randomness
103  * @requires vm.gc.Shenandoah
104  * @library /test/lib
105  *
106  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
107  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
108  *      TestAllocHumongousFragment
109  */
110 
111 /*
112  * @test id=compact
113  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
114  * @key randomness
115  * @requires vm.gc.Shenandoah
116  * @library /test/lib
117  *
118  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
119  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
120  *      TestAllocHumongousFragment
121  */
122 
123  /*
124  * @test id=g1
125  * @summary Make sure G1 can recover from humongous allocation fragmentation
126  * @key randomness
127  * @requires vm.gc.G1
128  * @library /test/lib
129  *
130  * @run main/othervm -Xlog:gc+region=trace -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g
131  *      -XX:VerifyGCType=full -XX:+VerifyDuringGC -XX:+VerifyAfterGC
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 }