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