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 * 29 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC -XX:+UseStringDeduplication -Xmx256M TestStringDedup 30 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC -XX:+UseStringDeduplication -Xmx256M TestStringDedup 31 * 32 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx256M TestStringDedup 33 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -XX:+UseStringDeduplication -Xmx256M TestStringDedup 34 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact -XX:+UseStringDeduplication -Xmx256M TestStringDedup 35 */ 36 37 import java.lang.reflect.*; 38 import java.util.*; 39 40 import sun.misc.*; 41 42 public class TestStringDedup { 43 private static Field valueField; 44 private static Unsafe unsafe; 45 46 private static final int UniqueStrings = 20; 47 48 static { 49 try { 50 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 51 field.setAccessible(true); 52 unsafe = (Unsafe) field.get(null); 53 54 valueField = String.class.getDeclaredField("value"); 55 valueField.setAccessible(true); 56 } catch (Exception e) { 57 throw new RuntimeException(e); 58 } 59 } 60 61 private static Object getValue(String string) { 62 try { 63 return valueField.get(string); 64 } catch (Exception e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 static class StringAndId { 70 private String str; 71 private int id; 72 73 public StringAndId(String str, int id) { 74 this.str = str; 75 this.id = id; 76 } 77 78 public String str() { 79 return str; 80 } 81 82 public int id() { 83 return id; 84 } 85 } 86 87 private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) { 88 Random rn = new Random(); 89 for (int u = 0; u < unique_strs; u++) { 90 int n = rn.nextInt() % 10; 91 n = Math.max(n, 2); 92 for (int index = 0; index < n; index++) { 93 strs.add(new StringAndId("Unique String " + u, u)); 94 } 95 } 96 } 97 98 private static int verifyDedepString(ArrayList<StringAndId> strs) { 99 HashMap<Object, StringAndId> seen = new HashMap<>(); 100 int total = 0; 101 int dedup = 0; 102 103 for (StringAndId item : strs) { 104 total++; 105 StringAndId existing_item = seen.get(getValue(item.str())); 106 if (existing_item == null) { 107 seen.put(getValue(item.str()), item); 108 } else { 109 if (item.id() != existing_item.id() || 110 !item.str().equals(existing_item.str())) { 111 System.out.println("StringDedup error:"); 112 System.out.println("String: " + item.str() + " != " + existing_item.str()); 113 throw new RuntimeException("StringDedup Test failed"); 114 } else { 115 dedup++; 116 } 117 } 118 } 119 System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup)); 120 return (total - dedup); 121 } 122 123 public static void main(String[] args) { 124 ArrayList<StringAndId> astrs = new ArrayList<>(); 125 generateStrings(astrs, UniqueStrings); 126 System.gc(); 127 System.gc(); 128 System.gc(); 129 System.gc(); 130 System.gc(); 131 132 if (verifyDedepString(astrs) != UniqueStrings) { 133 // Can not guarantee all strings are deduplicated, there can 134 // still have pending items in queues. 135 System.out.println("Not all strings are deduplicated"); 136 } 137 } 138 }