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