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