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