Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/TestStringDedup.java
32284 views
/*1* Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223/*24* @test TestStringDedup25* @summary Test Shenandoah string deduplication implementation26* @key gc27*28* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication29* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive30* -XX:+ShenandoahDegeneratedGC31* TestStringDedup32*33* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication34* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive35* -XX:-ShenandoahDegeneratedGC36* TestStringDedup37*/3839/*40* @test TestStringDedup41* @summary Test Shenandoah string deduplication implementation42* @key gc43*44* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication45* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact46* TestStringDedup47* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication48* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive49* TestStringDedup50*51* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication52* -XX:+UseShenandoahGC53* TestStringDedup54*55* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication56* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact57* TestStringDedup58*/5960/*61* @test TestStringDedup62* @summary Test Shenandoah string deduplication implementation63* @key gc64*65* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication66* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu67* TestStringDedup68*69* @run main/othervm -Xmx256m -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication70* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive71* TestStringDedup72*/7374import java.lang.reflect.*;75import java.util.*;7677import sun.misc.*;7879public class TestStringDedup {80private static Field valueField;81private static Unsafe unsafe;8283private static final int UniqueStrings = 20;8485static {86try {87Field field = Unsafe.class.getDeclaredField("theUnsafe");88field.setAccessible(true);89unsafe = (Unsafe) field.get(null);9091valueField = String.class.getDeclaredField("value");92valueField.setAccessible(true);93} catch (Exception e) {94throw new RuntimeException(e);95}96}9798private static Object getValue(String string) {99try {100return valueField.get(string);101} catch (Exception e) {102throw new RuntimeException(e);103}104}105106static class StringAndId {107private String str;108private int id;109110public StringAndId(String str, int id) {111this.str = str;112this.id = id;113}114115public String str() {116return str;117}118119public int id() {120return id;121}122}123124private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) {125Random rn = new Random();126for (int u = 0; u < unique_strs; u++) {127int n = rn.nextInt() % 10;128n = Math.max(n, 2);129for (int index = 0; index < n; index++) {130strs.add(new StringAndId("Unique String " + u, u));131}132}133}134135private static int verifyDedepString(ArrayList<StringAndId> strs) {136HashMap<Object, StringAndId> seen = new HashMap<>();137int total = 0;138int dedup = 0;139140for (StringAndId item : strs) {141total++;142StringAndId existing_item = seen.get(getValue(item.str()));143if (existing_item == null) {144seen.put(getValue(item.str()), item);145} else {146if (item.id() != existing_item.id() ||147!item.str().equals(existing_item.str())) {148System.out.println("StringDedup error:");149System.out.println("String: " + item.str() + " != " + existing_item.str());150throw new RuntimeException("StringDedup Test failed");151} else {152dedup++;153}154}155}156System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));157return (total - dedup);158}159160public static void main(String[] args) {161ArrayList<StringAndId> astrs = new ArrayList<>();162generateStrings(astrs, UniqueStrings);163System.gc();164System.gc();165System.gc();166System.gc();167System.gc();168169if (verifyDedepString(astrs) != UniqueStrings) {170// Can not guarantee all strings are deduplicated, there can171// still have pending items in queues.172System.out.println("Not all strings are deduplicated");173}174}175}176177178