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