1 /*
  2  * Copyright Amazon.com Inc. or its affiliates. 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 package gc.shenandoah.generational;
 26 
 27 import jdk.test.whitebox.WhiteBox;
 28 import java.util.Random;
 29 
 30 /*
 31  * @test id=generational
 32  * @requires vm.gc.Shenandoah
 33  * @summary Confirm that card marking and remembered set scanning do not crash.
 34  * @library /testlibrary /test/lib /
 35  * @build jdk.test.whitebox.WhiteBox
 36  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 37  * @run main/othervm -Xbootclasspath/a:.
 38  *      -XX:+IgnoreUnrecognizedVMOptions
 39  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 40  *      -XX:+UnlockExperimentalVMOptions
 41  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 42  *      gc.shenandoah.generational.TestSimpleGenerational
 43  */
 44 public class TestSimpleGenerational {
 45     private static WhiteBox WB = WhiteBox.getWhiteBox();
 46     private static final int RANDOM_SEED = 46;
 47     // Sequence of random numbers should end with same value
 48     private static final int EXPECTED_LAST_RANDOM = 136227050;
 49 
 50 
 51     public static class Node {
 52         private static final int NEIGHBOR_COUNT = 5;
 53         private static final int INT_ARRAY_SIZE = 8;
 54         private static final Random RANDOM = new Random(RANDOM_SEED);
 55 
 56         private int val;
 57         private Object objectField;
 58 
 59         // Each Node instance holds references to two "private" arrays.
 60         // One array holds raw seething bits (primitive integers) and the other
 61         // holds references.
 62 
 63         private int[] intsField;
 64         private Node [] neighbors;
 65 
 66         public Node(int val) {
 67             this.val = val;
 68             this.objectField = new Object();
 69             this.intsField = new int[INT_ARRAY_SIZE];
 70             this.intsField[0] = 0xca;
 71             this.intsField[1] = 0xfe;
 72             this.intsField[2] = 0xba;
 73             this.intsField[3] = 0xbe;
 74             this.intsField[4] = 0xba;
 75             this.intsField[5] = 0xad;
 76             this.intsField[6] = 0xba;
 77             this.intsField[7] = 0xbe;
 78 
 79             this.neighbors = new Node[NEIGHBOR_COUNT];
 80         }
 81 
 82         public int value() {
 83             return val;
 84         }
 85 
 86         // Copy each neighbor of n into a new node's neighbor array.
 87         // Then overwrite arbitrarily selected neighbor with newly allocated
 88         // leaf node.
 89         public static Node upheaval(Node n) {
 90             int firstValue = RANDOM.nextInt(Integer.MAX_VALUE);
 91             Node result = new Node(firstValue);
 92             if (n != null) {
 93                 for (int i = 0; i < NEIGHBOR_COUNT; i++) {
 94                     result.neighbors[i] = n.neighbors[i];
 95                 }
 96             }
 97             int secondValue = RANDOM.nextInt(Integer.MAX_VALUE);
 98             int overwriteIndex = firstValue % NEIGHBOR_COUNT;
 99             result.neighbors[overwriteIndex] = new Node(secondValue);
100             return result;
101         }
102     }
103 
104     public static void main(String args[]) throws Exception {
105         Node n = null;
106 
107         if (!WB.getBooleanVMFlag("UseShenandoahGC") || !WB.getStringVMFlag("ShenandoahGCMode").equals("generational")) {
108             throw new IllegalStateException("Command-line options not honored!");
109         }
110 
111         for (int count = 10000; count > 0; count--) {
112             n = Node.upheaval(n);
113         }
114 
115         System.out.println("Expected Last Random: [" + n.value() + "]");
116         if (n.value() != EXPECTED_LAST_RANDOM) {
117             throw new IllegalStateException("Random number sequence ended badly!");
118         }
119     }
120 }