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