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 randomness 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 randomness 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 randomness 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 import jdk.test.lib.Utils; 91 92 import sun.misc.*; 93 94 public class TestStringDedup { 95 private static Field valueField; 96 private static Unsafe unsafe; 97 98 private static final int UniqueStrings = 20; 99 100 static { 101 try { 102 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 103 field.setAccessible(true); 104 unsafe = (Unsafe) field.get(null); 105 106 valueField = String.class.getDeclaredField("value"); 107 valueField.setAccessible(true); 108 } catch (Exception e) { 109 throw new RuntimeException(e); 110 } 111 } 112 113 private static Object getValue(String string) { 114 try { 115 return valueField.get(string); 116 } catch (Exception e) { 117 throw new RuntimeException(e); 118 } 119 } 120 121 static class StringAndId { 122 private String str; 123 private int id; 124 125 public StringAndId(String str, int id) { 126 this.str = str; 127 this.id = id; 128 } 129 130 public String str() { 131 return str; 132 } 133 134 public int id() { 135 return id; 136 } 137 } 138 139 private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) { 140 Random rn = Utils.getRandomInstance(); 141 for (int u = 0; u < unique_strs; u++) { 142 int n = rn.nextInt() % 10; 143 n = Math.max(n, 2); 144 for (int index = 0; index < n; index++) { 145 strs.add(new StringAndId("Unique String " + u, u)); 146 } 147 } 148 } 149 150 private static int verifyDedepString(ArrayList<StringAndId> strs) { 151 HashMap<Object, StringAndId> seen = new HashMap<>(); 152 int total = 0; 153 int dedup = 0; 154 155 for (StringAndId item : strs) { 156 total++; 157 StringAndId existing_item = seen.get(getValue(item.str())); 158 if (existing_item == null) { 159 seen.put(getValue(item.str()), item); 160 } else { 161 if (item.id() != existing_item.id() || 162 !item.str().equals(existing_item.str())) { 163 System.out.println("StringDedup error:"); 164 System.out.println("String: " + item.str() + " != " + existing_item.str()); 165 throw new RuntimeException("StringDedup Test failed"); 166 } else { 167 dedup++; 168 } 169 } 170 } 171 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 172 return (total - dedup); 173 } 174 175 public static void main(String[] args) { 176 ArrayList<StringAndId> astrs = new ArrayList<>(); 177 generateStrings(astrs, UniqueStrings); 178 System.gc(); 179 System.gc(); 180 System.gc(); 181 System.gc(); 182 System.gc(); 183 184 if (verifyDedepString(astrs) != UniqueStrings) { 185 // Can not guarantee all strings are deduplicated, there can 186 // still have pending items in queues. 187 System.out.println("Not all strings are deduplicated"); 188 } 189 } 190 }