< prev index next >

test/hotspot/jtreg/gc/shenandoah/TestStringDedupStress.java

Print this page

  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  *

 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  /*
 76  * @test id=iu
 77  * @summary Test Shenandoah string deduplication implementation
 78  * @key randomness
 79  * @requires vm.gc.Shenandoah
 80  * @library /test/lib
 81  * @modules java.base/java.lang:open
 82  *          java.management
 83  *
 84  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 85  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu

 86  *      TestStringDedupStress
 87  *
 88  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 89  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 90  *      -DtargetStrings=2000000
 91  *      TestStringDedupStress
 92  *
 93  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 94  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 95  *      -XX:+ShenandoahOOMDuringEvacALot
 96  *      -DtargetStrings=2000000
 97  *      TestStringDedupStress
 98  *
 99  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
100  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
101  *      -XX:+ShenandoahOOMDuringEvacALot
102  *      -DtargetStrings=2000000
103  *      TestStringDedupStress
104  */
105 
106 import java.lang.management.*;
107 import java.lang.reflect.*;
108 import java.util.*;
109 import jdk.test.lib.Utils;
110 
111 public class TestStringDedupStress {
112     private static Field valueField;
113 
114     private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
115     private static final long MAX_REWRITE_GC_CYCLES = 6;
116     private static final long MAX_REWRITE_TIME = 30*1000; // ms
117 
118     private static final int UNIQUE_STRINGS = 20;
119 
120     static {
121         try {
122             valueField = String.class.getDeclaredField("value");
123             valueField.setAccessible(true);
124         } catch (Exception e) {
125             throw new RuntimeException(e);
126         }
127     }
128 
129     private static Object getValue(String string) {
130         try {
131             return valueField.get(string);
132         } catch (Exception e) {
133             throw new RuntimeException(e);
134         }
135     }
136 

194         Random rn = Utils.getRandomInstance();
195 
196         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
197             if ("Shenandoah Cycles".equals(bean.getName())) {
198                 gcCycleMBean = bean;
199                 break;
200             }
201         }
202 
203         if (gcCycleMBean == null) {
204             throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
205         }
206 
207         // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
208         int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
209         for (int index = 0; index < genIters; index++) {
210             generateStrings(astrs, UNIQUE_STRINGS);
211         }
212 
213         long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
214         long timeBeforeRewrite = System.currentTimeMillis();
215 
216         long loop = 1;
217         while (true) {
218             int arrSize = astrs.size();
219             int index = rn.nextInt(arrSize);
220             StringAndId item = astrs.get(index);
221             int n = rn.nextInt(UNIQUE_STRINGS);
222             item.str = "Unique String " + n;
223             item.id = n;
224 
225             if (loop++ % 1000 == 0) {
226                 // enough GC cycles for rewritten strings to be deduplicated
227                 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
228                     break;
229                 }
230 
231                 // enough time is spent waiting for GC to happen
232                 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {
233                     break;
234                 }
235             }
236         }
237         verifyDedupString(astrs);
238     }
239 }

  1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *

 27  * @test id=passive
 28  * @summary Test Shenandoah string deduplication implementation
 29  * @key randomness
 30  * @requires vm.gc.Shenandoah
 31  * @library /test/lib
 32  * @modules java.base/java.lang:open
 33  *          java.management
 34  *
 35  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 36  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 37  *      -XX:+ShenandoahDegeneratedGC
 38  *      TestStringDedupStress
 39  *
 40  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 41  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 42  *      -XX:-ShenandoahDegeneratedGC
 43  *      TestStringDedupStress
 44  */
 45 
 46 /*
 47  * @test id=generational
 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 -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 56  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 57  *      -XX:+ShenandoahDegeneratedGC
 58  *      -DtargetStrings=3000000
 59  *      TestStringDedupStress















 60  */
 61 
 62 /*
 63  * @test id=default
 64  * @summary Test Shenandoah string deduplication implementation
 65  * @key randomness
 66  * @requires vm.gc.Shenandoah
 67  * @library /test/lib
 68  * @modules java.base/java.lang:open
 69  *          java.management
 70  *
 71  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 72  *      -XX:+UseShenandoahGC
 73  *      -DtargetStrings=3000000
 74  *      TestStringDedupStress
 75  *
 76  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 77  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 78  *      -DtargetStrings=2000000
 79  *      TestStringDedupStress
 80  *
 81  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 82  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 83  *      -XX:+ShenandoahOOMDuringEvacALot
 84  *      -DtargetStrings=2000000
 85  *      TestStringDedupStress
 86  *
 87  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 88  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact


 89  *      TestStringDedupStress
 90  */
 91 
 92 import java.lang.management.*;
 93 import java.lang.reflect.*;
 94 import java.util.*;
 95 import jdk.test.lib.Utils;
 96 
 97 public class TestStringDedupStress {
 98     private static Field valueField;
 99 
100     private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
101     private static final long MAX_REWRITE_GC_CYCLES = 6;
102     private static final long MAX_REWRITE_TIME_NS = 30L * 1_000_000_000L; // 30s in ns
103 
104     private static final int UNIQUE_STRINGS = 20;
105 
106     static {
107         try {
108             valueField = String.class.getDeclaredField("value");
109             valueField.setAccessible(true);
110         } catch (Exception e) {
111             throw new RuntimeException(e);
112         }
113     }
114 
115     private static Object getValue(String string) {
116         try {
117             return valueField.get(string);
118         } catch (Exception e) {
119             throw new RuntimeException(e);
120         }
121     }
122 

180         Random rn = Utils.getRandomInstance();
181 
182         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
183             if ("Shenandoah Cycles".equals(bean.getName())) {
184                 gcCycleMBean = bean;
185                 break;
186             }
187         }
188 
189         if (gcCycleMBean == null) {
190             throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
191         }
192 
193         // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
194         int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
195         for (int index = 0; index < genIters; index++) {
196             generateStrings(astrs, UNIQUE_STRINGS);
197         }
198 
199         long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
200         long timeBeforeRewriteNanos = System.nanoTime();
201 
202         long loop = 1;
203         while (true) {
204             int arrSize = astrs.size();
205             int index = rn.nextInt(arrSize);
206             StringAndId item = astrs.get(index);
207             int n = rn.nextInt(UNIQUE_STRINGS);
208             item.str = "Unique String " + n;
209             item.id = n;
210 
211             if (loop++ % 1000 == 0) {
212                 // enough GC cycles for rewritten strings to be deduplicated
213                 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
214                     break;
215                 }
216 
217                 // enough time is spent waiting for GC to happen
218                 if (System.nanoTime() - timeBeforeRewriteNanos >= MAX_REWRITE_TIME_NS) {
219                     break;
220                 }
221             }
222         }
223         verifyDedupString(astrs);
224     }
225 }
< prev index next >