1 /*
  2  * Copyright (c) 2017, 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 id=passive
 27  * @summary Acceptance tests: collector can deal with retained objects
 28  * @key randomness
 29  * @requires vm.gc.Shenandoah
 30  * @library /test/lib
 31  *
 32  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 34  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 35  *      TestSieveObjects
 36  *
 37  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 39  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 40  *      TestSieveObjects
 41  *
 42  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 43  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 44  *      -XX:+ShenandoahDegeneratedGC
 45  *      TestSieveObjects
 46  *
 47  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 48  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 49  *      -XX:-ShenandoahDegeneratedGC
 50  *      TestSieveObjects
 51  */
 52 
 53 /*
 54  * @test id=aggressive
 55  * @summary Acceptance tests: collector can deal with retained objects
 56  * @key randomness
 57  * @requires vm.gc.Shenandoah
 58  * @library /test/lib
 59  *
 60  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 61  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 62  *      -XX:+ShenandoahOOMDuringEvacALot
 63  *      TestSieveObjects
 64  *
 65  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 66  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 67  *      -XX:+ShenandoahAllocFailureALot
 68  *      TestSieveObjects
 69  *
 70  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 72  *      TestSieveObjects
 73  */
 74 
 75 /*
 76  * @test id=adaptive
 77  * @summary Acceptance tests: collector can deal with retained objects
 78  * @key randomness
 79  * @requires vm.gc.Shenandoah
 80  * @library /test/lib
 81  *
 82  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 83  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 84  *      -XX:+ShenandoahVerify
 85  *      TestSieveObjects
 86  *
 87  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 88  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 89  *      TestSieveObjects
 90  *
 91  */
 92 
 93 /*
 94  * @test id=static
 95  * @summary Acceptance tests: collector can deal with retained objects
 96  * @key randomness
 97  * @requires vm.gc.Shenandoah
 98  * @library /test/lib
 99  *
100  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
101  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
102  *      TestSieveObjects
103  */
104 
105 /*
106  * @test id=compact
107  * @summary Acceptance tests: collector can deal with retained objects
108  * @key randomness
109  * @requires vm.gc.Shenandoah
110  * @library /test/lib
111  *
112  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
113  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
114  *      TestSieveObjects
115  */
116 
117 /*
118  * @test id=no-tlab
119  * @summary Acceptance tests: collector can deal with retained objects
120  * @key randomness
121  * @requires vm.gc.Shenandoah
122  * @library /test/lib
123  *
124  * @run main/othervm/timeout=240 -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
125  *      -XX:+UseShenandoahGC
126  *      -XX:-UseTLAB -XX:+ShenandoahVerify
127  *      TestSieveObjects
128  */
129 
130 import java.util.Random;
131 import jdk.test.lib.Utils;
132 
133 public class TestSieveObjects {
134 
135     static final int COUNT = 100_000_000;
136     static final int WINDOW = 1_000_000;
137     static final int PAYLOAD = 100;
138 
139     static final MyObject[] arr = new MyObject[WINDOW];
140 
141     public static void main(String[] args) throws Exception {
142         int rIdx = 0;
143         Random rng = Utils.getRandomInstance();
144         for (int c = 0; c < COUNT; c++) {
145             MyObject v = arr[rIdx];
146             if (v != null) {
147                 if (v.x != rIdx) {
148                     throw new IllegalStateException("Illegal value at index " + rIdx + ": " + v.x);
149                 }
150                 if (rng.nextInt(1000) > 100) {
151                     arr[rIdx] = null;
152                 }
153             } else {
154                 if (rng.nextInt(1000) > 500) {
155                     arr[rIdx] = new MyObject(rIdx);
156                 }
157             }
158             rIdx++;
159             if (rIdx >= WINDOW) {
160                 rIdx = 0;
161             }
162         }
163     }
164 
165     public static class MyObject {
166         public int x;
167         public byte[] payload;
168 
169         public MyObject(int x) {
170             this.x = x;
171             this.payload = new byte[PAYLOAD];
172         }
173     }
174 
175 }