1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24  /*
  25  * @test TestStringDedupStress
  26  * @summary Test Shenandoah string deduplication implementation
  27  * @key gc
  28  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  29  *                   -verbose:gc
  30  *                   -DtargetStrings=3000000
  31  *                   TestStringDedupStress
  32  *
  33  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  34  *                   -XX:ShenandoahGCHeuristics=aggressive -DtargetStrings=2000000
  35  *                   -verbose:gc
  36  *                   TestStringDedupStress
  37  *
  38  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  39  *                   -XX:ShenandoahGCHeuristics=aggressive -XX:+ShenandoahOOMDuringEvacALot -DtargetStrings=2000000
  40  *                   -verbose:gc
  41  *                    TestStringDedupStress
  42  *
  43  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  44  *                   -XX:ShenandoahGCHeuristics=static -DtargetStrings=4000000
  45  *                   -verbose:gc
  46  *                   TestStringDedupStress
  47  *
  48  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  49  *                   -XX:ShenandoahGCHeuristics=compact
  50  *                   -verbose:gc
  51  *                   TestStringDedupStress
  52  *
  53  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  54  *                   -XX:ShenandoahGCMode=passive -XX:+ShenandoahDegeneratedGC -DtargetOverwrites=40000000
  55  *                   -verbose:gc
  56  *                   TestStringDedupStress
  57  *
  58  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  59  *                   -XX:ShenandoahGCMode=passive -XX:-ShenandoahDegeneratedGC -DtargetOverwrites=40000000
  60  *                   -verbose:gc
  61  *                   TestStringDedupStress
  62  *
  63  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  64  *                   -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=3000000
  65  *                   -verbose:gc
  66  *                   TestStringDedupStress
  67  *
  68  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  69  *                   -XX:ShenandoahGCHeuristics=compact -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=2000000
  70  *                   -verbose:gc
  71  *                   TestStringDedupStress
  72  *
  73  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  74  *                   -XX:ShenandoahGCHeuristics=aggressive -XX:ShenandoahUpdateRefsEarly=off -DtargetStrings=2000000
  75  *                   -verbose:gc
  76  *                   TestStringDedupStress
  77  *
  78  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  79  *                   -XX:ShenandoahGCHeuristics=static -XX:ShenandoahUpdateRefsEarly=off -DtargetOverwrites=4000000
  80  *                   -verbose:gc
  81  *                   TestStringDedupStress
  82  *
  83  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:+UseStringDeduplication -Xmx1g
  84  *                   -XX:ShenandoahGCHeuristics=aggressive -XX:ShenandoahUpdateRefsEarly=off -XX:+ShenandoahOOMDuringEvacALot -DtargetStrings=2000000
  85  *                   -verbose:gc
  86  *                   TestStringDedupStress
  87  */
  88 
  89 import java.lang.management.*;
  90 import java.lang.reflect.*;
  91 import java.util.*;
  92 
  93 import sun.misc.*;
  94 
  95 public class TestStringDedupStress {
  96     private static Field valueField;
  97     private static Unsafe unsafe;
  98 
  99     private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
 100     private static final long MAX_REWRITE_GC_CYCLES = 6;
 101     private static final long MAX_REWRITE_TIME = 30*1000; // ms
 102 
 103     private static final int UNIQUE_STRINGS = 20;
 104 
 105     static {
 106         try {
 107             Field field = Unsafe.class.getDeclaredField("theUnsafe");
 108             field.setAccessible(true);
 109             unsafe = (Unsafe) field.get(null);
 110 
 111             valueField = String.class.getDeclaredField("value");
 112             valueField.setAccessible(true);
 113         } catch (Exception e) {
 114             throw new RuntimeException(e);
 115         }
 116     }
 117 
 118     private static Object getValue(String string) {
 119         try {
 120             return valueField.get(string);
 121         } catch (Exception e) {
 122             throw new RuntimeException(e);
 123         }
 124     }
 125 
 126     static class StringAndId {
 127         private String str;
 128         private int id;
 129 
 130         public StringAndId(String str, int id) {
 131             this.str = str;
 132             this.id = id;
 133         }
 134 
 135         public String str() {
 136             return str;
 137         }
 138 
 139         public int id() {
 140             return id;
 141         }
 142     }
 143 
 144     // Generate uniqueStrings number of strings
 145     private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {
 146         Random rn = new Random();
 147         for (int u = 0; u < uniqueStrings; u++) {
 148             int n = rn.nextInt(uniqueStrings);
 149             strs.add(new StringAndId("Unique String " + n, n));
 150         }
 151     }
 152 
 153     private static int verifyDedupString(ArrayList<StringAndId> strs) {
 154         Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);
 155         int total = 0;
 156         int dedup = 0;
 157 
 158         for (StringAndId item : strs) {
 159             total++;
 160             StringAndId existingItem = seen.get(getValue(item.str()));
 161             if (existingItem == null) {
 162                 seen.put(getValue(item.str()), item);
 163             } else {
 164                 if (item.id() != existingItem.id() ||
 165                         !item.str().equals(existingItem.str())) {
 166                     System.out.println("StringDedup error:");
 167                     System.out.println("id: " + item.id() + " != " + existingItem.id());
 168                     System.out.println("or String: " + item.str() + " != " + existingItem.str());
 169                     throw new RuntimeException("StringDedup Test failed");
 170                 } else {
 171                     dedup++;
 172                 }
 173             }
 174         }
 175         System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));
 176         return (total - dedup);
 177     }
 178 
 179     static volatile ArrayList<StringAndId> astrs = new ArrayList<>();
 180     static GarbageCollectorMXBean gcCycleMBean;
 181 
 182     public static void main(String[] args) {
 183         Random rn = new Random();
 184 
 185         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 186             if ("Shenandoah Cycles".equals(bean.getName())) {
 187                 gcCycleMBean = bean;
 188                 break;
 189             }
 190         }
 191 
 192         if (gcCycleMBean == null) {
 193             throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
 194         }
 195 
 196         // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
 197         int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
 198         for (int index = 0; index < genIters; index++) {
 199             generateStrings(astrs, UNIQUE_STRINGS);
 200         }
 201 
 202         long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
 203         long timeBeforeRewrite = System.currentTimeMillis();
 204 
 205         long loop = 1;
 206         while (true) {
 207             int arrSize = astrs.size();
 208             int index = rn.nextInt(arrSize);
 209             StringAndId item = astrs.get(index);
 210             int n = rn.nextInt(UNIQUE_STRINGS);
 211             item.str = "Unique String " + n;
 212             item.id = n;
 213 
 214             if (loop++ % 1000 == 0) {
 215                 // enough GC cycles for rewritten strings to be deduplicated
 216                 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
 217                     break;
 218                 }
 219 
 220                 // enough time is spent waiting for GC to happen
 221                 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {
 222                     break;
 223                 }
 224             }
 225         }
 226         verifyDedupString(astrs);
 227     }
 228 }