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 TestSimpleGenerational
 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   static private final int SeedForRandom = 46;
 47   // Sequence of random numbers should end with same value
 48   private static int ExpectedLastRandom = 272454100;
 49 
 50 
 51   public static class Node {
 52     static private final int NeighborCount = 5;
 53     static private final int IntArraySize = 8;
 54     static private Random random = new Random(SeedForRandom);
 55 
 56     private int val;
 57     private Object field_o;
 58 
 59     // Each Node instance holds references to two "private" arrays.
 60     // One array holds raw seething bits (primitive integers) and the
 61     // holds references.
 62 
 63     private int[] field_ints;
 64     private Node [] neighbors;
 65 
 66     public Node(int val) {
 67       this.val = val;
 68       this.field_o = new Object();
 69       this.field_ints = new int[IntArraySize];
 70       this.field_ints[0] = 0xca;
 71       this.field_ints[1] = 0xfe;
 72       this.field_ints[2] = 0xba;
 73       this.field_ints[3] = 0xbe;
 74       this.field_ints[4] = 0xba;
 75       this.field_ints[5] = 0xad;
 76       this.field_ints[6] = 0xba;
 77       this.field_ints[7] = 0xbe;
 78 
 79       this.neighbors = new Node[NeighborCount];
 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 first_val = random.nextInt();
 91       if (first_val < 0) first_val = -first_val;
 92       if (first_val < 0) first_val = 0;
 93       Node result = new Node(first_val);
 94       if (n != null) {
 95         for (int i = 0; i < NeighborCount; i++)
 96           result.neighbors[i] = n.neighbors[i];
 97       }
 98       int second_val = random.nextInt();
 99       if (second_val < 0) second_val = -second_val;
100       if (second_val < 0) second_val = 0;
101 
102       int overwrite_index = first_val % NeighborCount;
103       result.neighbors[overwrite_index] = new Node(second_val);
104       return result;
105     }
106   }
107 
108   public static void main(String args[]) throws Exception {
109     Node n = null;
110 
111     if (!wb.getBooleanVMFlag("UseShenandoahGC") ||
112         !wb.getStringVMFlag("ShenandoahGCMode").equals("generational"))
113       throw new IllegalStateException("Command-line options not honored!");
114 
115     for (int count = 10000; count > 0; count--) {
116       n = Node.upheaval(n);
117     }
118 
119     if (n.value() != ExpectedLastRandom)
120       throw new IllegalStateException("Random number sequence ended badly!");
121 
122   }
123 
124 }
125