1 /* 2 * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 /* 25 * @test TestStringDedupStress 26 * @summary Test Shenandoah string deduplication implementation 27 * @key gc 28 * @requires vm.gc.Shenandoah & !vm.graal.enabled 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc:open 31 * @modules java.base/java.lang:open 32 * java.management 33 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 34 * -DtargetStrings=3000000 35 * -Xlog:gc 36 * TestStringDedupStress 37 * 38 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 39 * -XX:ShenandoahGCHeuristics=aggressive -DtargetStrings=2000000 40 * -Xlog:gc 41 * TestStringDedupStress 42 * 43 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 44 * -XX:ShenandoahGCHeuristics=aggressive -XX:+ShenandoahOOMDuringEvacALot -DtargetStrings=2000000 45 * -Xlog:gc 46 * TestStringDedupStress 47 * 48 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 49 * -XX:ShenandoahGCHeuristics=static -DtargetStrings=4000000 50 * -Xlog:gc 51 * TestStringDedupStress 52 * 53 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 54 * -XX:ShenandoahGCHeuristics=compact 55 * -Xlog:gc 56 * TestStringDedupStress 57 * 58 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 59 * -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC 60 * -Xlog:gc 61 * TestStringDedupStress 62 * 63 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 64 * -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC 65 * -Xlog:gc 66 * TestStringDedupStress 67 * 68 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 69 * -XX:ShenandoahGCHeuristics=traversal 70 * -Xlog:gc 71 * TestStringDedupStress 72 * 73 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 74 * -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=3000000 75 * -Xlog:gc 76 * TestStringDedupStress 77 * 78 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 79 * -XX:ShenandoahGCHeuristics=compact -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=2000000 80 * -Xlog:gc 81 * TestStringDedupStress 82 * 83 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 84 * -XX:ShenandoahGCHeuristics=aggressive -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=2000000 85 * -Xlog:gc 86 * TestStringDedupStress 87 * 88 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 89 * -XX:ShenandoahGCHeuristics=static -XX:ShenandoahUpdateRefsEarly=off 90 * -Xlog:gc 91 * TestStringDedupStress 92 * 93 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 94 * -XX:ShenandoahGCHeuristics=aggressive -XX:ShenandoahUpdateRefsEarly=off -XX:+ShenandoahOOMDuringEvacALot -DtargetStrings=2000000 95 * -Xlog:gc 96 * TestStringDedupStress 97 * 98 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g -Xlog:gc+stats 99 * -XX:ShenandoahGCHeuristics=traversal -XX:+ShenandoahOOMDuringEvacALot -DtargetStrings=2000000 100 * -Xlog:gc 101 * TestStringDedupStress 102 */ 103 104 import java.lang.management.*; 105 import java.lang.reflect.*; 106 import java.util.*; 107 108 import sun.misc.*; 109 110 public class TestStringDedupStress { 111 private static Field valueField; 112 private static Unsafe unsafe; 113 114 private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000); 115 private static final long MAX_REWRITE_GC_CYCLES = 6; 116 private static final long MAX_REWRITE_TIME = 30*1000; // ms 117 118 private static final int UNIQUE_STRINGS = 20; 119 120 static { 121 try { 122 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 123 field.setAccessible(true); 124 unsafe = (Unsafe) field.get(null); 125 126 valueField = String.class.getDeclaredField("value"); 127 valueField.setAccessible(true); 128 } catch (Exception e) { 129 throw new RuntimeException(e); 130 } 131 } 132 133 private static Object getValue(String string) { 134 try { 135 return valueField.get(string); 136 } catch (Exception e) { 137 throw new RuntimeException(e); 138 } 139 } 140 141 static class StringAndId { 142 private String str; 143 private int id; 144 145 public StringAndId(String str, int id) { 146 this.str = str; 147 this.id = id; 148 } 149 150 public String str() { 151 return str; 152 } 153 154 public int id() { 155 return id; 156 } 157 } 158 159 // Generate uniqueStrings number of strings 160 private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) { 161 Random rn = new Random(); 162 for (int u = 0; u < uniqueStrings; u++) { 163 int n = rn.nextInt(uniqueStrings); 164 strs.add(new StringAndId("Unique String " + n, n)); 165 } 166 } 167 168 private static int verifyDedupString(ArrayList<StringAndId> strs) { 169 Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2); 170 int total = 0; 171 int dedup = 0; 172 173 for (StringAndId item : strs) { 174 total++; 175 StringAndId existingItem = seen.get(getValue(item.str())); 176 if (existingItem == null) { 177 seen.put(getValue(item.str()), item); 178 } else { 179 if (item.id() != existingItem.id() || 180 !item.str().equals(existingItem.str())) { 181 System.out.println("StringDedup error:"); 182 System.out.println("id: " + item.id() + " != " + existingItem.id()); 183 System.out.println("or String: " + item.str() + " != " + existingItem.str()); 184 throw new RuntimeException("StringDedup Test failed"); 185 } else { 186 dedup++; 187 } 188 } 189 } 190 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 191 return (total - dedup); 192 } 193 194 static volatile ArrayList<StringAndId> astrs = new ArrayList<>(); 195 static GarbageCollectorMXBean gcCycleMBean; 196 197 public static void main(String[] args) { 198 Random rn = new Random(); 199 200 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { 201 if ("Shenandoah Cycles".equals(bean.getName())) { 202 gcCycleMBean = bean; 203 break; 204 } 205 } 206 207 if (gcCycleMBean == null) { 208 throw new RuntimeException("Can not find Shenandoah GC cycle mbean"); 209 } 210 211 // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique 212 int genIters = TARGET_STRINGS / UNIQUE_STRINGS; 213 for (int index = 0; index < genIters; index++) { 214 generateStrings(astrs, UNIQUE_STRINGS); 215 } 216 217 long cycleBeforeRewrite = gcCycleMBean.getCollectionCount(); 218 long timeBeforeRewrite = System.currentTimeMillis(); 219 220 long loop = 1; 221 while (true) { 222 int arrSize = astrs.size(); 223 int index = rn.nextInt(arrSize); 224 StringAndId item = astrs.get(index); 225 int n = rn.nextInt(UNIQUE_STRINGS); 226 item.str = "Unique String " + n; 227 item.id = n; 228 229 if (loop++ % 1000 == 0) { 230 // enough GC cycles for rewritten strings to be deduplicated 231 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) { 232 break; 233 } 234 235 // enough time is spent waiting for GC to happen 236 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) { 237 break; 238 } 239 } 240 } 241 verifyDedupString(astrs); 242 } 243 }