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