1 /* 2 * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 /* 25 * @test TestStringDedup 26 * @summary Test Shenandoah string deduplication implementation 27 * @key gc 28 * @requires vm.gc.Shenandoah & !vm.graal.enabled 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc:open 31 * @modules java.base/java.lang:open 32 * java.management 33 * 34 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 35 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 36 * -XX:+ShenandoahDegeneratedGC 37 * TestStringDedup 38 * 39 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 40 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 41 * -XX:-ShenandoahDegeneratedGC 42 * TestStringDedup 43 */ 44 45 /* 46 * @test TestStringDedup 47 * @summary Test Shenandoah string deduplication implementation 48 * @key gc 49 * @requires vm.gc.Shenandoah & !vm.graal.enabled 50 * @library /test/lib 51 * @modules java.base/jdk.internal.misc:open 52 * @modules java.base/java.lang:open 53 * java.management 54 * 55 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 56 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 57 * TestStringDedup 58 * 59 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 60 * -XX:+UseShenandoahGC 61 * TestStringDedup 62 * 63 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 64 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact 65 * TestStringDedup 66 */ 67 68 /* 69 * @test TestStringDedup 70 * @summary Test Shenandoah string deduplication implementation 71 * @key gc 72 * @requires vm.gc.Shenandoah & !vm.graal.enabled 73 * @library /test/lib 74 * @modules java.base/jdk.internal.misc:open 75 * @modules java.base/java.lang:open 76 * java.management 77 * 78 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 79 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=traversal 80 * TestStringDedup 81 * 82 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 83 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=traversal -XX:ShenandoahGCHeuristics=aggressive 84 * TestStringDedup 85 */ 86 87 import java.lang.reflect.*; 88 import java.util.*; 89 90 import sun.misc.*; 91 92 public class TestStringDedup { 93 private static Field valueField; 94 private static Unsafe unsafe; 95 96 private static final int UniqueStrings = 20; 97 98 static { 99 try { 100 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 101 field.setAccessible(true); 102 unsafe = (Unsafe) field.get(null); 103 104 valueField = String.class.getDeclaredField("value"); 105 valueField.setAccessible(true); 106 } catch (Exception e) { 107 throw new RuntimeException(e); 108 } 109 } 110 111 private static Object getValue(String string) { 112 try { 113 return valueField.get(string); 114 } catch (Exception e) { 115 throw new RuntimeException(e); 116 } 117 } 118 119 static class StringAndId { 120 private String str; 121 private int id; 122 123 public StringAndId(String str, int id) { 124 this.str = str; 125 this.id = id; 126 } 127 128 public String str() { 129 return str; 130 } 131 132 public int id() { 133 return id; 134 } 135 } 136 137 private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) { 138 Random rn = new Random(); 139 for (int u = 0; u < unique_strs; u++) { 140 int n = rn.nextInt() % 10; 141 n = Math.max(n, 2); 142 for (int index = 0; index < n; index++) { 143 strs.add(new StringAndId("Unique String " + u, u)); 144 } 145 } 146 } 147 148 private static int verifyDedepString(ArrayList<StringAndId> strs) { 149 HashMap<Object, StringAndId> seen = new HashMap<>(); 150 int total = 0; 151 int dedup = 0; 152 153 for (StringAndId item : strs) { 154 total++; 155 StringAndId existing_item = seen.get(getValue(item.str())); 156 if (existing_item == null) { 157 seen.put(getValue(item.str()), item); 158 } else { 159 if (item.id() != existing_item.id() || 160 !item.str().equals(existing_item.str())) { 161 System.out.println("StringDedup error:"); 162 System.out.println("String: " + item.str() + " != " + existing_item.str()); 163 throw new RuntimeException("StringDedup Test failed"); 164 } else { 165 dedup++; 166 } 167 } 168 } 169 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 170 return (total - dedup); 171 } 172 173 public static void main(String[] args) { 174 ArrayList<StringAndId> astrs = new ArrayList<>(); 175 generateStrings(astrs, UniqueStrings); 176 System.gc(); 177 System.gc(); 178 System.gc(); 179 System.gc(); 180 System.gc(); 181 182 if (verifyDedepString(astrs) != UniqueStrings) { 183 // Can not guarantee all strings are deduplicated, there can 184 // still have pending items in queues. 185 System.out.println("Not all strings are deduplicated"); 186 } 187 } 188 }