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=g1
143  * @summary Make sure G1 can recover from humongous allocation fragmentation
144  * @key randomness
145  * @requires vm.gc.G1
146  * @library /test/lib
147  *
148  * @run main/othervm -Xlog:gc+region=trace -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g
149  *      -XX:VerifyGCType=full -XX:+VerifyDuringGC -XX:+VerifyAfterGC
150  *      TestAllocHumongousFragment
151  */
152 
153 import java.util.*;
154 import jdk.test.lib.Utils;
155 
156 public class TestAllocHumongousFragment {
157 
158     static final long TARGET_MB = Long.getLong("target", 30_000); // 30 Gb allocations
159     static final long LIVE_MB   = Long.getLong("occupancy", 700); // 700 Mb alive
160 
161     static volatile Object sink;
162 
163     static List<int[]> objects;
164 
165     public static void main(String[] args) throws Exception {
166         final int min = 128 * 1024;
167         final int max = 16 * 1024 * 1024;
168         final long count = TARGET_MB * 1024 * 1024 / (16 + 4 * (min + (max - min) / 2));
169 
170         objects = new ArrayList<>();
171         long current = 0;
172 
173         Random rng = Utils.getRandomInstance();
174         for (long c = 0; c < count; c++) {
175             while (current > LIVE_MB * 1024 * 1024) {
176                 int idx = rng.nextInt(objects.size());
177                 int[] remove = objects.remove(idx);
178                 current -= remove.length * 4 + 16;
179             }
180 
181             int[] newObj = new int[min + rng.nextInt(max - min)];
182             current += newObj.length * 4 + 16;
183             objects.add(newObj);
184             sink = new Object();
185 
186             System.out.println("Allocated: " + (current / 1024 / 1024) + " Mb");
187         }
188     }
189 
190 }