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 import java.util.HashMap;
 30 
 31 /*
 32  *  To avoid the risk of false regressions identified by this test, the heap
 33  *  size is set artificially high.  Though this test is known to run reliably
 34  *  in 66 MB heap, the heap size for this test run is currently set to 256 MB.
 35  */
 36 
 37 /*
 38  * @test TestConcurrentEvac
 39  * @requires vm.gc.Shenandoah
 40  * @summary Confirm that card marking and remembered set scanning do not crash.
 41  * @library /testlibrary /test/lib /
 42  * @build jdk.test.whitebox.WhiteBox
 43  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 44  * @run main/othervm -Xbootclasspath/a:.
 45  *      -Xms256m -Xmx256m
 46  *      -XX:+IgnoreUnrecognizedVMOptions
 47  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 48  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 49  *      -XX:NewRatio=1 -XX:+UnlockExperimentalVMOptions
 50  *      -XX:ShenandoahGuaranteedGCInterval=3000
 51  *      -XX:-UseDynamicNumberOfGCThreads -XX:-ShenandoahPacing
 52  *      gc.shenandoah.generational.TestConcurrentEvac
 53  */
 54 
 55 public class TestConcurrentEvac {
 56   private static WhiteBox wb = WhiteBox.getWhiteBox();
 57 
 58   static private final int SeedForRandom = 46;
 59   // Sequence of random numbers should end with same value
 60 
 61   // Smaller table will cause creation of more old-gen garbage
 62   // as previous entries in table are overwritten with new values.
 63   static private final int TableSize = 53;
 64   static private final int MaxStringLength = 47;
 65   static private final int SentenceLength = 5;
 66 
 67   static private Random random = new Random(SeedForRandom);
 68 
 69   public static class Node {
 70     static private final int NeighborCount = 48;
 71     static private final int ChildOverwriteCount = 32;
 72     static private final int IntArraySize = 128;
 73 
 74     private String name;
 75 
 76     // Each Node instance holds an array containing all substrings of
 77     // its name
 78 
 79     // This array has entries from 0 .. (name.length() - 1).
 80     // num_substrings[i] represents the number of substrings that
 81     // correspond to a name of length i+1.
 82     private static int [] num_substrings;
 83 
 84     static {
 85       // Initialize num_substrings.
 86       // For a name of length N, there are
 87       //  N substrings of length 1
 88       //  N-1 substrings of length 2
 89       //  N-2 substrings of length 3
 90       //  ...
 91       //  1 substring of length N
 92       // Note that:
 93       //   num_substrings[0] = 1
 94       //   num_substrings[1] = 3
 95       //   num_substrings[i] = (i+1)+num_substrings[i-1]
 96 
 97       num_substrings = new int[MaxStringLength];
 98       num_substrings[0] = 1;
 99       for (int i = 1; i < MaxStringLength; i++)
100         num_substrings[i] = (i+1)+num_substrings[i-1];
101     }
102 
103     private String [] substrings;
104     private Node [] neighbors;
105 
106     public Node(String name) {
107       this.name = name;
108       this.substrings = new String[num_substrings[name.length() - 1]];
109 
110       int index = 0;
111       for (int substring_length = 1;
112            substring_length <= name.length(); substring_length++) {
113         for (int offset = 0;
114              offset + substring_length <= name.length(); offset++) {
115           this.substrings[index++] = name.substring(offset,
116                                                     offset + substring_length);
117         }
118       }
119     }
120 
121     public String value() {
122       return name;
123     }
124 
125     public String arbitrary_substring() {
126       int index = TestConcurrentEvac.randomUnsignedInt() % substrings.length;
127       return substrings[index];
128     }
129   }
130 
131 
132   // Return random int between 1 and MaxStringLength inclusive
133   static int randomStringLength() {
134     int length = randomUnsignedInt();
135     length %= (MaxStringLength - 1);
136     length += 1;
137     return length;
138   }
139 
140   static String randomCharacter() {
141     int index = randomUnsignedInt() % 52;
142     return ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
143             substring(index, index+1));
144   }
145 
146   static String randomString() {
147     int length = randomStringLength();
148     String result = new String(); // make the compiler work for this garbage...
149     for (int i = 0; i < length; i++)
150       result += randomCharacter();
151     return result;
152   }
153 
154   static int randomUnsignedInt() {
155     int result = random.nextInt();
156     if (result < 0) result = -result;
157     if (result < 0) result = 0;
158     return result;
159   }
160 
161   static int randomIndex() {
162     int index = randomUnsignedInt();
163     index %= TableSize;
164     return index;
165   }
166 
167   public static void main(String args[]) throws Exception {
168     HashMap<Integer, Node> table = new HashMap<Integer, Node>(TableSize);
169 
170     if (!wb.getBooleanVMFlag("UseShenandoahGC") ||
171         !wb.getStringVMFlag("ShenandoahGCMode").equals("generational"))
172       throw new IllegalStateException("Command-line options not honored!");
173 
174     for (int count = java.lang.Integer.MAX_VALUE/1024; count >= 0; count--) {
175       int index = randomIndex();
176       String name = randomString();
177       table.put(index, new Node(name));
178     }
179 
180     String conclusion = "";
181 
182     for (int i = 0; i < SentenceLength; i++) {
183       Node a_node = table.get(randomIndex());
184       if (a_node == null)
185         i--;
186       else {
187         String a_string = a_node.arbitrary_substring();
188         conclusion += a_string;
189         conclusion += " ";
190       }
191     }
192     conclusion = conclusion.substring(0, conclusion.length() - 1);
193 
194     System.out.println("Conclusion is [" + conclusion + "]");
195 
196     if (!conclusion.equals("cTy cTykJ kAkKAOWYEHbxFCmRIlyk xjYMdNmtAQXNGdIc sqHKsWnJIP"))
197       throw new IllegalStateException("Random sequence of words did not end well!");
198 
199   }
200 }
201