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  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 /*
 28  * @test id=passive
 29  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 30  * @key randomness
 31  * @requires vm.gc.Shenandoah
 32  * @library /test/lib
 33  *
 34  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 36  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 37  *      TestAllocHumongousFragment
 38  *
 39  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 40  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 41  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 42  *      TestAllocHumongousFragment
 43  *
 44  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 45  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 46  *      -XX:-ShenandoahDegeneratedGC
 47  *      TestAllocHumongousFragment
 48  *
 49  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 50  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 51  *      -XX:+ShenandoahDegeneratedGC
 52  *      TestAllocHumongousFragment
 53  */
 54 
 55 /*
 56  * @test id=aggressive
 57  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 58  * @key randomness
 59  * @requires vm.gc.Shenandoah
 60  * @library /test/lib
 61  *
 62  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 63  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 64  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
 65  *      TestAllocHumongousFragment
 66  *
 67  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 68  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 69  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
 70  *      TestAllocHumongousFragment
 71  *
 72  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 73  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 74  *      -XX:+ShenandoahOOMDuringEvacALot
 75  *      TestAllocHumongousFragment
 76  *
 77  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 78  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 79  *      -XX:+ShenandoahAllocFailureALot
 80  *      TestAllocHumongousFragment
 81  */
 82 
 83 /*
 84  * @test id=adaptive
 85  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
 86  * @key randomness
 87  * @requires vm.gc.Shenandoah
 88  * @library /test/lib
 89  *
 90  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 91  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 92  *      -XX:+ShenandoahVerify
 93  *      TestAllocHumongousFragment
 94  *
 95  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
 96  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 97  *      TestAllocHumongousFragment
 98 */
 99 
100 /*
101  * @test id=generational
102  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
103  * @key randomness
104  * @requires vm.gc.Shenandoah
105  * @library /test/lib
106  *
107  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
108  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive -XX:ShenandoahGCMode=generational
109  *      -XX:+ShenandoahVerify
110  *      TestAllocHumongousFragment
111  *
112  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
113  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive -XX:ShenandoahGCMode=generational
114  *      TestAllocHumongousFragment
115  */
116 
117 /*
118  * @test id=static
119  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
120  * @key randomness
121  * @requires vm.gc.Shenandoah
122  * @library /test/lib
123  *
124  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
125  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
126  *      TestAllocHumongousFragment
127  */
128 
129 /*
130  * @test id=compact
131  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
132  * @key randomness
133  * @requires vm.gc.Shenandoah
134  * @library /test/lib
135  *
136  * @run main/othervm -Xmx1g -Xms1g -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahTargetNumRegions=2048
137  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
138  *      TestAllocHumongousFragment
139  */
140 
141 /*
142  * @test id=iu-aggressive
143  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
144  * @key randomness
145  * @requires vm.gc.Shenandoah
146  * @library /test/lib
147  *
148  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
149  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
150  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
151  *      TestAllocHumongousFragment
152  *
153  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
154  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
155  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
156  *      TestAllocHumongousFragment
157  *
158  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
159  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
160  *      -XX:+ShenandoahOOMDuringEvacALot
161  *      TestAllocHumongousFragment
162  *
163  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
164  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
165  *      -XX:+ShenandoahAllocFailureALot
166  *      TestAllocHumongousFragment
167  */
168 
169 /*
170  * @test id=iu
171  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
172  * @key randomness
173  * @requires vm.gc.Shenandoah
174  * @library /test/lib
175  *
176  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
177  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
178  *      -XX:+ShenandoahVerify
179  *      TestAllocHumongousFragment
180  *
181  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
182  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
183  *      TestAllocHumongousFragment
184  */
185 
186  /*
187  * @test id=g1
188  * @summary Make sure G1 can recover from humongous allocation fragmentation
189  * @key randomness
190  * @requires vm.gc.G1
191  * @library /test/lib
192  *
193  * @run main/othervm -Xlog:gc+region=trace -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g
194  *      -XX:VerifyGCType=full -XX:+VerifyDuringGC -XX:+VerifyAfterGC
195  *      TestAllocHumongousFragment
196  */
197 
198 import java.util.*;
199 import jdk.test.lib.Utils;
200 
201 public class TestAllocHumongousFragment {
202 
203     static final long TARGET_MB = Long.getLong("target", 30_000); // 30 Gb allocations
204     static final long LIVE_MB   = Long.getLong("occupancy", 700); // 700 Mb alive
205 
206     static volatile Object sink;
207 
208     static List<int[]> objects;
209 
210     public static void main(String[] args) throws Exception {
211         final int min = 128 * 1024;
212         final int max = 16 * 1024 * 1024;
213         final long count = TARGET_MB * 1024 * 1024 / (16 + 4 * (min + (max - min) / 2));
214 
215         objects = new ArrayList<>();
216         long current = 0;
217 
218         Random rng = Utils.getRandomInstance();
219         for (long c = 0; c < count; c++) {
220             while (current > LIVE_MB * 1024 * 1024) {
221                 int idx = rng.nextInt(objects.size());
222                 int[] remove = objects.remove(idx);
223                 current -= remove.length * 4 + 16;
224             }
225 
226             int[] newObj = new int[min + rng.nextInt(max - min)];
227             current += newObj.length * 4 + 16;
228             objects.add(newObj);
229             sink = new Object();
230 
231             System.out.println("Allocated: " + (current / 1024 / 1024) + " Mb");
232         }
233     }
234 
235 }