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 import java.lang.management.*; 93 import java.lang.reflect.*; 94 import java.util.*; 95 import jdk.test.lib.Utils; 96 97 public class TestStringDedupStress { 98 private static Field valueField; 99 100 private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000); 101 private static final long MAX_REWRITE_GC_CYCLES = 6; 102 private static final long MAX_REWRITE_TIME_NS = 30L * 1_000_000_000L; // 30s in ns 103 104 private static final int UNIQUE_STRINGS = 20; 105 106 static { 107 try { 108 valueField = String.class.getDeclaredField("value"); 109 valueField.setAccessible(true); 110 } catch (Exception e) { 111 throw new RuntimeException(e); 112 } 113 } 114 115 private static Object getValue(String string) { 116 try { 117 return valueField.get(string); 118 } catch (Exception e) { 119 throw new RuntimeException(e); 120 } 121 } 122 123 static class StringAndId { 124 private String str; 125 private int id; 126 127 public StringAndId(String str, int id) { 128 this.str = str; 129 this.id = id; 130 } 131 132 public String str() { 133 return str; 134 } 135 136 public int id() { 137 return id; 138 } 139 } 140 141 // Generate uniqueStrings number of strings 142 private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) { 143 Random rn = Utils.getRandomInstance(); 144 for (int u = 0; u < uniqueStrings; u++) { 145 int n = rn.nextInt(uniqueStrings); 146 strs.add(new StringAndId("Unique String " + n, n)); 147 } 148 } 149 150 private static int verifyDedupString(ArrayList<StringAndId> strs) { 151 Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2); 152 int total = 0; 153 int dedup = 0; 154 155 for (StringAndId item : strs) { 156 total++; 157 StringAndId existingItem = seen.get(getValue(item.str())); 158 if (existingItem == null) { 159 seen.put(getValue(item.str()), item); 160 } else { 161 if (item.id() != existingItem.id() || 162 !item.str().equals(existingItem.str())) { 163 System.out.println("StringDedup error:"); 164 System.out.println("id: " + item.id() + " != " + existingItem.id()); 165 System.out.println("or String: " + item.str() + " != " + existingItem.str()); 166 throw new RuntimeException("StringDedup Test failed"); 167 } else { 168 dedup++; 169 } 170 } 171 } 172 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 173 return (total - dedup); 174 } 175 176 static volatile ArrayList<StringAndId> astrs = new ArrayList<>(); 177 static GarbageCollectorMXBean gcCycleMBean; 178 179 public static void main(String[] args) { 180 Random rn = Utils.getRandomInstance(); 181 182 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { 183 if ("Shenandoah Cycles".equals(bean.getName())) { 184 gcCycleMBean = bean; 185 break; 186 } 187 } 188 189 if (gcCycleMBean == null) { 190 throw new RuntimeException("Can not find Shenandoah GC cycle mbean"); 191 } 192 193 // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique 194 int genIters = TARGET_STRINGS / UNIQUE_STRINGS; 195 for (int index = 0; index < genIters; index++) { 196 generateStrings(astrs, UNIQUE_STRINGS); 197 } 198 199 long cycleBeforeRewrite = gcCycleMBean.getCollectionCount(); 200 long timeBeforeRewriteNanos = System.nanoTime(); 201 202 long loop = 1; 203 while (true) { 204 int arrSize = astrs.size(); 205 int index = rn.nextInt(arrSize); 206 StringAndId item = astrs.get(index); 207 int n = rn.nextInt(UNIQUE_STRINGS); 208 item.str = "Unique String " + n; 209 item.id = n; 210 211 if (loop++ % 1000 == 0) { 212 // enough GC cycles for rewritten strings to be deduplicated 213 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) { 214 break; 215 } 216 217 // enough time is spent waiting for GC to happen 218 if (System.nanoTime() - timeBeforeRewriteNanos >= MAX_REWRITE_TIME_NS) { 219 break; 220 } 221 } 222 } 223 verifyDedupString(astrs); 224 } 225 }