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=iu-aggressive
125  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
126  * @key randomness
127  * @requires vm.gc.Shenandoah
128  * @library /test/lib
129  *
130  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
131  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
132  *      -XX:+ShenandoahOOMDuringEvacALot -XX:+ShenandoahVerify
133  *      TestAllocHumongousFragment
134  *
135  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
136  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
137  *      -XX:+ShenandoahAllocFailureALot -XX:+ShenandoahVerify
138  *      TestAllocHumongousFragment
139  *
140  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
141  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
142  *      -XX:+ShenandoahOOMDuringEvacALot
143  *      TestAllocHumongousFragment
144  *
145  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
146  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
147  *      -XX:+ShenandoahAllocFailureALot
148  *      TestAllocHumongousFragment
149  */
150 
151 /*
152  * @test id=iu
153  * @summary Make sure Shenandoah can recover from humongous allocation fragmentation
154  * @key randomness
155  * @requires vm.gc.Shenandoah
156  * @library /test/lib
157  *
158  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
159  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
160  *      -XX:+ShenandoahVerify
161  *      TestAllocHumongousFragment
162  *
163  * @run main/othervm -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g -XX:ShenandoahTargetNumRegions=2048
164  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
165  *      TestAllocHumongousFragment
166  */
167 
168  /*
169  * @test id=g1
170  * @summary Make sure G1 can recover from humongous allocation fragmentation
171  * @key randomness
172  * @requires vm.gc.G1
173  * @library /test/lib
174  *
175  * @run main/othervm -Xlog:gc+region=trace -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g
176  *      -XX:VerifyGCType=full -XX:+VerifyDuringGC -XX:+VerifyAfterGC
177  *      TestAllocHumongousFragment
178  */
179 
180 import java.util.*;
181 import jdk.test.lib.Utils;
182 
183 public class TestAllocHumongousFragment {
184 
185     static final long TARGET_MB = Long.getLong("target", 30_000); // 30 Gb allocations
186     static final long LIVE_MB   = Long.getLong("occupancy", 700); // 700 Mb alive
187 
188     static volatile Object sink;
189 
190     static List<int[]> objects;
191 
192     public static void main(String[] args) throws Exception {
193         final int min = 128 * 1024;
194         final int max = 16 * 1024 * 1024;
195         final long count = TARGET_MB * 1024 * 1024 / (16 + 4 * (min + (max - min) / 2));
196 
197         objects = new ArrayList<>();
198         long current = 0;
199 
200         Random rng = Utils.getRandomInstance();
201         for (long c = 0; c < count; c++) {
202             while (current > LIVE_MB * 1024 * 1024) {
203                 int idx = rng.nextInt(objects.size());
204                 int[] remove = objects.remove(idx);
205                 current -= remove.length * 4 + 16;
206             }
207 
208             int[] newObj = new int[min + rng.nextInt(max - min)];
209             current += newObj.length * 4 + 16;
210             objects.add(newObj);
211             sink = new Object();
212 
213             System.out.println("Allocated: " + (current / 1024 / 1024) + " Mb");
214         }
215     }
216 
217 }