1 /* 2 * Copyright (c) 2017, 2021, 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 id=passive 27 * @summary Test Shenandoah string deduplication implementation 28 * @key randomness 29 * @requires vm.gc.Shenandoah 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 -DGCCount=1 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 -DGCCount=1 43 * TestStringDedup 44 */ 45 46 /* 47 * @test id=default 48 * @summary Test Shenandoah string deduplication implementation 49 * @key randomness 50 * @requires vm.gc.Shenandoah 51 * @library /test/lib 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 -XX:StringDeduplicationAgeThreshold=3 57 * TestStringDedup 58 * 59 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 60 * -XX:+UseShenandoahGC -XX:StringDeduplicationAgeThreshold=3 61 * TestStringDedup 62 * 63 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 64 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact -XX:StringDeduplicationAgeThreshold=3 65 * TestStringDedup 66 */ 67 68 /* 69 * @test id=generational 70 * @summary Test Shenandoah string deduplication implementation 71 * @key randomness 72 * @requires vm.gc.Shenandoah 73 * @library /test/lib 74 * @modules java.base/java.lang:open 75 * java.management 76 * 77 * @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication 78 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -XX:StringDeduplicationAgeThreshold=3 79 * TestStringDedup 80 */ 81 82 import java.lang.reflect.*; 83 import java.util.*; 84 import jdk.test.lib.Utils; 85 86 public class TestStringDedup { 87 private static Field valueField; 88 89 private static final int UniqueStrings = 20; 90 // How many GC cycles are needed to complete deduplication. 91 private static final int GCCount = Integer.getInteger("GCCount", 3); 92 93 static { 94 try { 95 valueField = String.class.getDeclaredField("value"); 96 valueField.setAccessible(true); 97 } catch (Exception e) { 98 throw new RuntimeException(e); 99 } 100 } 101 102 private static Object getValue(String string) { 103 try { 104 return valueField.get(string); 105 } catch (Exception e) { 106 throw new RuntimeException(e); 107 } 108 } 109 110 static class StringAndId { 111 private String str; 112 private int id; 113 114 public StringAndId(String str, int id) { 115 this.str = str; 116 this.id = id; 117 } 118 119 public String str() { 120 return str; 121 } 122 123 public int id() { 124 return id; 125 } 126 } 127 128 private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) { 129 Random rn = Utils.getRandomInstance(); 130 for (int u = 0; u < unique_strs; u++) { 131 int n = rn.nextInt() % 10; 132 n = Math.max(n, 2); 133 for (int index = 0; index < n; index++) { 134 strs.add(new StringAndId("Unique String " + u, u)); 135 } 136 } 137 } 138 139 private static int verifyDedepString(ArrayList<StringAndId> strs) { 140 HashMap<Object, StringAndId> seen = new HashMap<>(); 141 int total = 0; 142 int dedup = 0; 143 144 for (StringAndId item : strs) { 145 total++; 146 StringAndId existing_item = seen.get(getValue(item.str())); 147 if (existing_item == null) { 148 seen.put(getValue(item.str()), item); 149 } else { 150 if (item.id() != existing_item.id() || 151 !item.str().equals(existing_item.str())) { 152 System.out.println("StringDedup error:"); 153 System.out.println("String: " + item.str() + " != " + existing_item.str()); 154 throw new RuntimeException("StringDedup Test failed"); 155 } else { 156 dedup++; 157 } 158 } 159 } 160 return (total - dedup); 161 } 162 163 public static void main(String[] args) { 164 ArrayList<StringAndId> astrs = new ArrayList<>(); 165 generateStrings(astrs, UniqueStrings); 166 for (int count = 0; count < GCCount; count ++) { 167 System.gc(); 168 } 169 170 int unique_count = 0; 171 for (int waitCount = 0; waitCount < 3; waitCount ++) { 172 // Let concurrent string dedup thread to run 173 try { 174 Thread.sleep(1000); 175 } catch (InterruptedException e) { 176 } 177 178 // All deduplicated, done. 179 unique_count = verifyDedepString(astrs); 180 if ( unique_count == UniqueStrings) { 181 return; 182 } 183 } 184 185 throw new RuntimeException("Expecting " + UniqueStrings + " unique strings, but got " + unique_count); 186 } 187 }