1 /*
   2  * Copyright (c) 2017, 2018, 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 TestStringDedupStress
  27  * @summary Test Shenandoah string deduplication implementation
  28  * @key gc
  29  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  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 -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 TestStringDedupStress
  48  * @summary Test Shenandoah string deduplication implementation
  49  * @key gc
  50  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  51  * @library /test/lib
  52  * @modules java.base/jdk.internal.misc:open
  53  * @modules java.base/java.lang:open
  54  *          java.management
  55  *
  56  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  57  *      -XX:+UseShenandoahGC
  58  *      -DtargetStrings=3000000
  59  *      TestStringDedupStress
  60  *
  61  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  62  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  63  *      -DtargetStrings=2000000
  64  *      TestStringDedupStress
  65  *
  66  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  67  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  68  *      -XX:+ShenandoahOOMDuringEvacALot
  69  *      -DtargetStrings=2000000
  70  *      TestStringDedupStress
  71  *
  72  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  73  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  74  *      TestStringDedupStress
  75  */
  76 
  77  /*
  78  * @test TestStringDedupStress
  79  * @summary Test Shenandoah string deduplication implementation
  80  * @key gc
  81  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  82  * @library /test/lib
  83  * @modules java.base/jdk.internal.misc:open
  84  * @modules java.base/java.lang:open
  85  *          java.management
  86  *
  87  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  88  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  89  *      TestStringDedupStress
  90  *
  91  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  92  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
  93  *      -DtargetStrings=2000000
  94  *      TestStringDedupStress
  95  *
  96  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  97  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  98  *      -XX:+ShenandoahOOMDuringEvacALot
  99  *      -DtargetStrings=2000000
 100  *      TestStringDedupStress
 101  *
 102  * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
 103  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
 104  *      -XX:+ShenandoahOOMDuringEvacALot
 105  *      -DtargetStrings=2000000
 106  *      TestStringDedupStress
 107  */
 108 
 109 import java.lang.management.*;
 110 import java.lang.reflect.*;
 111 import java.util.*;
 112 
 113 import sun.misc.*;
 114 
 115 public class TestStringDedupStress {
 116     private static Field valueField;
 117     private static Unsafe unsafe;
 118 
 119     private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
 120     private static final long MAX_REWRITE_GC_CYCLES = 6;
 121     private static final long MAX_REWRITE_TIME = 30*1000; // ms
 122 
 123     private static final int UNIQUE_STRINGS = 20;
 124 
 125     static {
 126         try {
 127             Field field = Unsafe.class.getDeclaredField("theUnsafe");
 128             field.setAccessible(true);
 129             unsafe = (Unsafe) field.get(null);
 130 
 131             valueField = String.class.getDeclaredField("value");
 132             valueField.setAccessible(true);
 133         } catch (Exception e) {
 134             throw new RuntimeException(e);
 135         }
 136     }
 137 
 138     private static Object getValue(String string) {
 139         try {
 140             return valueField.get(string);
 141         } catch (Exception e) {
 142             throw new RuntimeException(e);
 143         }
 144     }
 145 
 146     static class StringAndId {
 147         private String str;
 148         private int id;
 149 
 150         public StringAndId(String str, int id) {
 151             this.str = str;
 152             this.id = id;
 153         }
 154 
 155         public String str() {
 156             return str;
 157         }
 158 
 159         public int id() {
 160             return id;
 161         }
 162     }
 163 
 164     // Generate uniqueStrings number of strings
 165     private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {
 166         Random rn = new Random();
 167         for (int u = 0; u < uniqueStrings; u++) {
 168             int n = rn.nextInt(uniqueStrings);
 169             strs.add(new StringAndId("Unique String " + n, n));
 170         }
 171     }
 172 
 173     private static int verifyDedupString(ArrayList<StringAndId> strs) {
 174         Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);
 175         int total = 0;
 176         int dedup = 0;
 177 
 178         for (StringAndId item : strs) {
 179             total++;
 180             StringAndId existingItem = seen.get(getValue(item.str()));
 181             if (existingItem == null) {
 182                 seen.put(getValue(item.str()), item);
 183             } else {
 184                 if (item.id() != existingItem.id() ||
 185                         !item.str().equals(existingItem.str())) {
 186                     System.out.println("StringDedup error:");
 187                     System.out.println("id: " + item.id() + " != " + existingItem.id());
 188                     System.out.println("or String: " + item.str() + " != " + existingItem.str());
 189                     throw new RuntimeException("StringDedup Test failed");
 190                 } else {
 191                     dedup++;
 192                 }
 193             }
 194         }
 195         System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));
 196         return (total - dedup);
 197     }
 198 
 199     static volatile ArrayList<StringAndId> astrs = new ArrayList<>();
 200     static GarbageCollectorMXBean gcCycleMBean;
 201 
 202     public static void main(String[] args) {
 203         Random rn = new Random();
 204 
 205         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 206             if ("Shenandoah Cycles".equals(bean.getName())) {
 207                 gcCycleMBean = bean;
 208                 break;
 209             }
 210         }
 211 
 212         if (gcCycleMBean == null) {
 213             throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
 214         }
 215 
 216         // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
 217         int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
 218         for (int index = 0; index < genIters; index++) {
 219             generateStrings(astrs, UNIQUE_STRINGS);
 220         }
 221 
 222         long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
 223         long timeBeforeRewrite = System.currentTimeMillis();
 224 
 225         long loop = 1;
 226         while (true) {
 227             int arrSize = astrs.size();
 228             int index = rn.nextInt(arrSize);
 229             StringAndId item = astrs.get(index);
 230             int n = rn.nextInt(UNIQUE_STRINGS);
 231             item.str = "Unique String " + n;
 232             item.id = n;
 233 
 234             if (loop++ % 1000 == 0) {
 235                 // enough GC cycles for rewritten strings to be deduplicated
 236                 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
 237                     break;
 238                 }
 239 
 240                 // enough time is spent waiting for GC to happen
 241                 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {
 242                     break;
 243                 }
 244             }
 245         }
 246         verifyDedupString(astrs);
 247     }
 248 }