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