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 TestStringDedupStress 27 * @summary Test Shenandoah string deduplication implementation 28 * @key randomness 29 * @requires vm.gc.Shenandoah & !vm.graal.enabled 30 * @library /test/lib 31 * @modules java.base/jdk.internal.misc:open 32 * @modules java.base/java.lang:open 33 * java.management 34 * 35 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 36 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 37 * -XX:+ShenandoahDegeneratedGC 38 * TestStringDedupStress 39 * 40 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 41 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 42 * -XX:-ShenandoahDegeneratedGC 43 * TestStringDedupStress 44 */ 45 46 /* 47 * @test TestStringDedupStress 48 * @summary Test Shenandoah string deduplication implementation 49 * @key randomness 50 * @requires vm.gc.Shenandoah & !vm.graal.enabled 51 * @library /test/lib 52 * @modules java.base/jdk.internal.misc:open 53 * @modules java.base/java.lang:open 54 * java.management 55 * 56 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 57 * -XX:+UseShenandoahGC 58 * -DtargetStrings=3000000 59 * TestStringDedupStress 60 * 61 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 62 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 63 * -DtargetStrings=2000000 64 * TestStringDedupStress 65 * 66 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 67 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 68 * -XX:+ShenandoahOOMDuringEvacALot 69 * -DtargetStrings=2000000 70 * TestStringDedupStress 71 * 72 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 73 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact 74 * TestStringDedupStress 75 */ 76 77 /* 78 * @test TestStringDedupStress 79 * @summary Test Shenandoah string deduplication implementation 80 * @key randomness 81 * @requires vm.gc.Shenandoah & !vm.graal.enabled 82 * @library /test/lib 83 * @modules java.base/jdk.internal.misc:open 84 * @modules java.base/java.lang:open 85 * java.management 86 * 87 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 88 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 89 * TestStringDedupStress 90 * 91 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 92 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive 93 * -DtargetStrings=2000000 94 * TestStringDedupStress 95 * 96 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 97 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 98 * -XX:+ShenandoahOOMDuringEvacALot 99 * -DtargetStrings=2000000 100 * TestStringDedupStress 101 * 102 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 103 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive 104 * -XX:+ShenandoahOOMDuringEvacALot 105 * -DtargetStrings=2000000 106 * TestStringDedupStress 107 */ 108 109 import java.lang.management.*; 110 import java.lang.reflect.*; 111 import java.util.*; 112 import jdk.test.lib.Utils; 113 114 import sun.misc.*; 115 116 public class TestStringDedupStress { 117 private static Field valueField; 118 private static Unsafe unsafe; 119 120 private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000); 121 private static final long MAX_REWRITE_GC_CYCLES = 6; 122 private static final long MAX_REWRITE_TIME = 30*1000; // ms 123 124 private static final int UNIQUE_STRINGS = 20; 125 126 static { 127 try { 128 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 129 field.setAccessible(true); 130 unsafe = (Unsafe) field.get(null); 131 132 valueField = String.class.getDeclaredField("value"); 133 valueField.setAccessible(true); 134 } catch (Exception e) { 135 throw new RuntimeException(e); 136 } 137 } 138 139 private static Object getValue(String string) { 140 try { 141 return valueField.get(string); 142 } catch (Exception e) { 143 throw new RuntimeException(e); 144 } 145 } 146 147 static class StringAndId { 148 private String str; 149 private int id; 150 151 public StringAndId(String str, int id) { 152 this.str = str; 153 this.id = id; 154 } 155 156 public String str() { 157 return str; 158 } 159 160 public int id() { 161 return id; 162 } 163 } 164 165 // Generate uniqueStrings number of strings 166 private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) { 167 Random rn = Utils.getRandomInstance(); 168 for (int u = 0; u < uniqueStrings; u++) { 169 int n = rn.nextInt(uniqueStrings); 170 strs.add(new StringAndId("Unique String " + n, n)); 171 } 172 } 173 174 private static int verifyDedupString(ArrayList<StringAndId> strs) { 175 Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2); 176 int total = 0; 177 int dedup = 0; 178 179 for (StringAndId item : strs) { 180 total++; 181 StringAndId existingItem = seen.get(getValue(item.str())); 182 if (existingItem == null) { 183 seen.put(getValue(item.str()), item); 184 } else { 185 if (item.id() != existingItem.id() || 186 !item.str().equals(existingItem.str())) { 187 System.out.println("StringDedup error:"); 188 System.out.println("id: " + item.id() + " != " + existingItem.id()); 189 System.out.println("or String: " + item.str() + " != " + existingItem.str()); 190 throw new RuntimeException("StringDedup Test failed"); 191 } else { 192 dedup++; 193 } 194 } 195 } 196 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 197 return (total - dedup); 198 } 199 200 static volatile ArrayList<StringAndId> astrs = new ArrayList<>(); 201 static GarbageCollectorMXBean gcCycleMBean; 202 203 public static void main(String[] args) { 204 Random rn = Utils.getRandomInstance(); 205 206 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { 207 if ("Shenandoah Cycles".equals(bean.getName())) { 208 gcCycleMBean = bean; 209 break; 210 } 211 } 212 213 if (gcCycleMBean == null) { 214 throw new RuntimeException("Can not find Shenandoah GC cycle mbean"); 215 } 216 217 // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique 218 int genIters = TARGET_STRINGS / UNIQUE_STRINGS; 219 for (int index = 0; index < genIters; index++) { 220 generateStrings(astrs, UNIQUE_STRINGS); 221 } 222 223 long cycleBeforeRewrite = gcCycleMBean.getCollectionCount(); 224 long timeBeforeRewrite = System.currentTimeMillis(); 225 226 long loop = 1; 227 while (true) { 228 int arrSize = astrs.size(); 229 int index = rn.nextInt(arrSize); 230 StringAndId item = astrs.get(index); 231 int n = rn.nextInt(UNIQUE_STRINGS); 232 item.str = "Unique String " + n; 233 item.id = n; 234 235 if (loop++ % 1000 == 0) { 236 // enough GC cycles for rewritten strings to be deduplicated 237 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) { 238 break; 239 } 240 241 // enough time is spent waiting for GC to happen 242 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) { 243 break; 244 } 245 } 246 } 247 verifyDedupString(astrs); 248 } 249 }