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 TestStringDedup
  26  * @summary Test Shenandoah string deduplication implementation
  27  * @key gc
  28  *
  29  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  30  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  31  *      -XX:+ShenandoahDegeneratedGC
  32  *      TestStringDedup
  33  *
  34  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  36  *      -XX:-ShenandoahDegeneratedGC
  37  *      TestStringDedup
  38  */
  39 
  40 /*
  41  * @test TestStringDedup
  42  * @summary Test Shenandoah string deduplication implementation
  43  * @key gc
  44  *
  45  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  46  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  47  *      TestStringDedup
  48  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  49  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  50  *      TestStringDedup
  51  *
  52  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  53  *      -XX:+UseShenandoahGC
  54  *      TestStringDedup
  55  *
  56  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  57  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  58  *      TestStringDedup
  59  */
  60 
  61 /*
  62  * @test TestStringDedup
  63  * @summary Test Shenandoah string deduplication implementation
  64  * @key gc
  65  *
  66  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  67  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  68  *      TestStringDedup
  69  *
  70  * @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
  71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
  72  *      TestStringDedup
  73  */
  74 
  75 import java.lang.reflect.*;
  76 import java.util.*;
  77 
  78 import sun.misc.*;
  79 
  80 public class TestStringDedup {
  81     private static Field valueField;
  82     private static Unsafe unsafe;
  83 
  84     private static final int UniqueStrings = 20;
  85 
  86     static {
  87         try {
  88             Field field = Unsafe.class.getDeclaredField("theUnsafe");
  89             field.setAccessible(true);
  90             unsafe = (Unsafe) field.get(null);
  91 
  92             valueField = String.class.getDeclaredField("value");
  93             valueField.setAccessible(true);
  94         } catch (Exception e) {
  95             throw new RuntimeException(e);
  96         }
  97     }
  98 
  99     private static Object getValue(String string) {
 100         try {
 101             return valueField.get(string);
 102         } catch (Exception e) {
 103             throw new RuntimeException(e);
 104         }
 105     }
 106 
 107     static class StringAndId {
 108         private String str;
 109         private int id;
 110 
 111         public StringAndId(String str, int id) {
 112             this.str = str;
 113             this.id = id;
 114         }
 115 
 116         public String str() {
 117             return str;
 118         }
 119 
 120         public int id() {
 121             return id;
 122         }
 123     }
 124 
 125     private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) {
 126         Random rn = new Random();
 127         for (int u = 0; u < unique_strs; u++) {
 128             int n = rn.nextInt() % 10;
 129             n = Math.max(n, 2);
 130             for (int index = 0; index < n; index++) {
 131                 strs.add(new StringAndId("Unique String " + u, u));
 132             }
 133         }
 134     }
 135 
 136     private static int verifyDedepString(ArrayList<StringAndId> strs) {
 137         HashMap<Object, StringAndId> seen = new HashMap<>();
 138         int total = 0;
 139         int dedup = 0;
 140 
 141         for (StringAndId item : strs) {
 142             total++;
 143             StringAndId existing_item = seen.get(getValue(item.str()));
 144             if (existing_item == null) {
 145                 seen.put(getValue(item.str()), item);
 146             } else {
 147                 if (item.id() != existing_item.id() ||
 148                         !item.str().equals(existing_item.str())) {
 149                     System.out.println("StringDedup error:");
 150                     System.out.println("String: " + item.str() + " != " + existing_item.str());
 151                     throw new RuntimeException("StringDedup Test failed");
 152                 } else {
 153                     dedup++;
 154                 }
 155             }
 156         }
 157         System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));
 158         return (total - dedup);
 159     }
 160 
 161     public static void main(String[] args) {
 162         ArrayList<StringAndId> astrs = new ArrayList<>();
 163         generateStrings(astrs, UniqueStrings);
 164         System.gc();
 165         System.gc();
 166         System.gc();
 167         System.gc();
 168         System.gc();
 169 
 170         if (verifyDedepString(astrs) != UniqueStrings) {
 171             // Can not guarantee all strings are deduplicated, there can
 172             // still have pending items in queues.
 173             System.out.println("Not all strings are deduplicated");
 174         }
 175     }
 176 }