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/java.lang:open
 32  *          java.management
 33  *
 34  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 36  *      -XX:+ShenandoahDegeneratedGC
 37  *      TestStringDedupStress
 38  *
 39  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 40  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 41  *      -XX:-ShenandoahDegeneratedGC
 42  *      TestStringDedupStress
 43  */
 44 
 45 /*
 46  * @test id=default
 47  * @summary Test Shenandoah string deduplication implementation
 48  * @key randomness
 49  * @requires vm.gc.Shenandoah
 50  * @library /test/lib
 51  * @modules java.base/java.lang:open
 52  *          java.management
 53  *
 54  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 55  *      -XX:+UseShenandoahGC
 56  *      -DtargetStrings=3000000
 57  *      TestStringDedupStress
 58  *
 59  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 60  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 61  *      -DtargetStrings=2000000
 62  *      TestStringDedupStress
 63  *
 64  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 65  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 66  *      -XX:+ShenandoahOOMDuringEvacALot
 67  *      -DtargetStrings=2000000
 68  *      TestStringDedupStress
 69  *
 70  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
 72  *      TestStringDedupStress
 73  */
 74 
 75 import java.lang.management.*;
 76 import java.lang.reflect.*;
 77 import java.util.*;
 78 import jdk.test.lib.Utils;
 79 
 80 public class TestStringDedupStress {
 81     private static Field valueField;
 82 
 83     private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
 84     private static final long MAX_REWRITE_GC_CYCLES = 6;
 85     private static final long MAX_REWRITE_TIME_NS = 30L * 1_000_000_000L; // 30s in ns
 86 
 87     private static final int UNIQUE_STRINGS = 20;
 88 
 89     static {
 90         try {
 91             valueField = String.class.getDeclaredField("value");
 92             valueField.setAccessible(true);
 93         } catch (Exception e) {
 94             throw new RuntimeException(e);
 95         }
 96     }
 97 
 98     private static Object getValue(String string) {
 99         try {
100             return valueField.get(string);
101         } catch (Exception e) {
102             throw new RuntimeException(e);
103         }
104     }
105 
106     static class StringAndId {
107         private String str;
108         private int id;
109 
110         public StringAndId(String str, int id) {
111             this.str = str;
112             this.id = id;
113         }
114 
115         public String str() {
116             return str;
117         }
118 
119         public int id() {
120             return id;
121         }
122     }
123 
124     // Generate uniqueStrings number of strings
125     private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {
126         Random rn = Utils.getRandomInstance();
127         for (int u = 0; u < uniqueStrings; u++) {
128             int n = rn.nextInt(uniqueStrings);
129             strs.add(new StringAndId("Unique String " + n, n));
130         }
131     }
132 
133     private static int verifyDedupString(ArrayList<StringAndId> strs) {
134         Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);
135         int total = 0;
136         int dedup = 0;
137 
138         for (StringAndId item : strs) {
139             total++;
140             StringAndId existingItem = seen.get(getValue(item.str()));
141             if (existingItem == null) {
142                 seen.put(getValue(item.str()), item);
143             } else {
144                 if (item.id() != existingItem.id() ||
145                         !item.str().equals(existingItem.str())) {
146                     System.out.println("StringDedup error:");
147                     System.out.println("id: " + item.id() + " != " + existingItem.id());
148                     System.out.println("or String: " + item.str() + " != " + existingItem.str());
149                     throw new RuntimeException("StringDedup Test failed");
150                 } else {
151                     dedup++;
152                 }
153             }
154         }
155         System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));
156         return (total - dedup);
157     }
158 
159     static volatile ArrayList<StringAndId> astrs = new ArrayList<>();
160     static GarbageCollectorMXBean gcCycleMBean;
161 
162     public static void main(String[] args) {
163         Random rn = Utils.getRandomInstance();
164 
165         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
166             if ("Shenandoah Cycles".equals(bean.getName())) {
167                 gcCycleMBean = bean;
168                 break;
169             }
170         }
171 
172         if (gcCycleMBean == null) {
173             throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
174         }
175 
176         // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
177         int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
178         for (int index = 0; index < genIters; index++) {
179             generateStrings(astrs, UNIQUE_STRINGS);
180         }
181 
182         long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
183         long timeBeforeRewriteNanos = System.nanoTime();
184 
185         long loop = 1;
186         while (true) {
187             int arrSize = astrs.size();
188             int index = rn.nextInt(arrSize);
189             StringAndId item = astrs.get(index);
190             int n = rn.nextInt(UNIQUE_STRINGS);
191             item.str = "Unique String " + n;
192             item.id = n;
193 
194             if (loop++ % 1000 == 0) {
195                 // enough GC cycles for rewritten strings to be deduplicated
196                 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
197                     break;
198                 }
199 
200                 // enough time is spent waiting for GC to happen
201                 if (System.nanoTime() - timeBeforeRewriteNanos >= MAX_REWRITE_TIME_NS) {
202                     break;
203                 }
204             }
205         }
206         verifyDedupString(astrs);
207     }
208 }