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 TestStringDedup 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 -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 36 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 37 * -XX:+ShenandoahDegeneratedGC 38 * TestStringDedup 39 * 40 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 41 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 42 * -XX:-ShenandoahDegeneratedGC 43 * TestStringDedup 44 */ 45 46 /* 47 * @test TestStringDedup 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 -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 57 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 58 * TestStringDedup 59 * 60 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 61 * -XX:+UseShenandoahGC 62 * TestStringDedup 63 * 64 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 65 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact 66 * TestStringDedup 67 */ 68 69 /* 70 * @test TestStringDedup 71 * @summary Test Shenandoah string deduplication implementation 72 * @key gc 73 * @requires vm.gc.Shenandoah & !vm.graal.enabled 74 * @library /test/lib 75 * @modules java.base/jdk.internal.misc:open 76 * @modules java.base/java.lang:open 77 * java.management 78 * 79 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 80 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 81 * TestStringDedup 82 * 83 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 84 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive 85 * TestStringDedup 86 */ 87 88 import java.lang.reflect.*; 89 import java.util.*; 90 91 import sun.misc.*; 92 93 public class TestStringDedup { 94 private static Field valueField; 95 private static Unsafe unsafe; 96 97 private static final int UniqueStrings = 20; 98 99 static { 100 try { 101 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 102 field.setAccessible(true); 103 unsafe = (Unsafe) field.get(null); 104 105 valueField = String.class.getDeclaredField("value"); 106 valueField.setAccessible(true); 107 } catch (Exception e) { 108 throw new RuntimeException(e); 109 } 110 } 111 112 private static Object getValue(String string) { 113 try { 114 return valueField.get(string); 115 } catch (Exception e) { 116 throw new RuntimeException(e); 117 } 118 } 119 120 static class StringAndId { 121 private String str; 122 private int id; 123 124 public StringAndId(String str, int id) { 125 this.str = str; 126 this.id = id; 127 } 128 129 public String str() { 130 return str; 131 } 132 133 public int id() { 134 return id; 135 } 136 } 137 138 private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) { 139 Random rn = new Random(); 140 for (int u = 0; u < unique_strs; u++) { 141 int n = rn.nextInt() % 10; 142 n = Math.max(n, 2); 143 for (int index = 0; index < n; index++) { 144 strs.add(new StringAndId("Unique String " + u, u)); 145 } 146 } 147 } 148 149 private static int verifyDedepString(ArrayList<StringAndId> strs) { 150 HashMap<Object, StringAndId> seen = new HashMap<>(); 151 int total = 0; 152 int dedup = 0; 153 154 for (StringAndId item : strs) { 155 total++; 156 StringAndId existing_item = seen.get(getValue(item.str())); 157 if (existing_item == null) { 158 seen.put(getValue(item.str()), item); 159 } else { 160 if (item.id() != existing_item.id() || 161 !item.str().equals(existing_item.str())) { 162 System.out.println("StringDedup error:"); 163 System.out.println("String: " + item.str() + " != " + existing_item.str()); 164 throw new RuntimeException("StringDedup Test failed"); 165 } else { 166 dedup++; 167 } 168 } 169 } 170 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 171 return (total - dedup); 172 } 173 174 public static void main(String[] args) { 175 ArrayList<StringAndId> astrs = new ArrayList<>(); 176 generateStrings(astrs, UniqueStrings); 177 System.gc(); 178 System.gc(); 179 System.gc(); 180 System.gc(); 181 System.gc(); 182 183 if (verifyDedepString(astrs) != UniqueStrings) { 184 // Can not guarantee all strings are deduplicated, there can 185 // still have pending items in queues. 186 System.out.println("Not all strings are deduplicated"); 187 } 188 } 189 }