Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/TestStringDedupStress.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 TestStringDedupStress25* @summary Test Shenandoah string deduplication implementation26* @key gc27*28* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication29* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive30* -XX:+ShenandoahDegeneratedGC31* TestStringDedupStress32*33* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication34* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive35* -XX:-ShenandoahDegeneratedGC36* TestStringDedupStress37*/3839/*40* @test TestStringDedupStress41* @summary Test Shenandoah string deduplication implementation42* @key gc43*44* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication45* -XX:+UseShenandoahGC46* -DtargetStrings=300000047* TestStringDedupStress48*49* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication50* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive51* -DtargetStrings=200000052* TestStringDedupStress53*54* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication55* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive56* -XX:+ShenandoahOOMDuringEvacALot57* -DtargetStrings=200000058* TestStringDedupStress59*60* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication61* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact62* TestStringDedupStress63*/6465/*66* @test TestStringDedupStress67* @summary Test Shenandoah string deduplication implementation68* @key gc69*70* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication71* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu72* TestStringDedupStress73*74* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication75* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive76* -DtargetStrings=200000077* TestStringDedupStress78*79* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication80* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu81* -XX:+ShenandoahOOMDuringEvacALot82* -DtargetStrings=200000083* TestStringDedupStress84*85* @run main/othervm -Xmx1g -verbose:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication86* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive87* -XX:+ShenandoahOOMDuringEvacALot88* -DtargetStrings=200000089* TestStringDedupStress90*/9192import java.lang.management.*;93import java.lang.reflect.*;94import java.util.*;9596import sun.misc.*;9798public class TestStringDedupStress {99private static Field valueField;100private static Unsafe unsafe;101102private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);103private static final long MAX_REWRITE_GC_CYCLES = 6;104private static final long MAX_REWRITE_TIME = 30*1000; // ms105106private static final int UNIQUE_STRINGS = 20;107108static {109try {110Field field = Unsafe.class.getDeclaredField("theUnsafe");111field.setAccessible(true);112unsafe = (Unsafe) field.get(null);113114valueField = String.class.getDeclaredField("value");115valueField.setAccessible(true);116} catch (Exception e) {117throw new RuntimeException(e);118}119}120121private static Object getValue(String string) {122try {123return valueField.get(string);124} catch (Exception e) {125throw new RuntimeException(e);126}127}128129static class StringAndId {130private String str;131private int id;132133public StringAndId(String str, int id) {134this.str = str;135this.id = id;136}137138public String str() {139return str;140}141142public int id() {143return id;144}145}146147// Generate uniqueStrings number of strings148private static void generateStrings(ArrayList<StringAndId> strs, int uniqueStrings) {149Random rn = new Random();150for (int u = 0; u < uniqueStrings; u++) {151int n = rn.nextInt(uniqueStrings);152strs.add(new StringAndId("Unique String " + n, n));153}154}155156private static int verifyDedupString(ArrayList<StringAndId> strs) {157Map<Object, StringAndId> seen = new HashMap<>(TARGET_STRINGS*2);158int total = 0;159int dedup = 0;160161for (StringAndId item : strs) {162total++;163StringAndId existingItem = seen.get(getValue(item.str()));164if (existingItem == null) {165seen.put(getValue(item.str()), item);166} else {167if (item.id() != existingItem.id() ||168!item.str().equals(existingItem.str())) {169System.out.println("StringDedup error:");170System.out.println("id: " + item.id() + " != " + existingItem.id());171System.out.println("or String: " + item.str() + " != " + existingItem.str());172throw new RuntimeException("StringDedup Test failed");173} else {174dedup++;175}176}177}178System.out.println("Dedup: " + dedup + "/" + total + " unique: " + (total - dedup));179return (total - dedup);180}181182static volatile ArrayList<StringAndId> astrs = new ArrayList<>();183static GarbageCollectorMXBean gcCycleMBean;184185public static void main(String[] args) {186Random rn = new Random();187188for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {189if ("Shenandoah Cycles".equals(bean.getName())) {190gcCycleMBean = bean;191break;192}193}194195if (gcCycleMBean == null) {196throw new RuntimeException("Can not find Shenandoah GC cycle mbean");197}198199// Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique200int genIters = TARGET_STRINGS / UNIQUE_STRINGS;201for (int index = 0; index < genIters; index++) {202generateStrings(astrs, UNIQUE_STRINGS);203}204205long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();206long timeBeforeRewrite = System.currentTimeMillis();207208long loop = 1;209while (true) {210int arrSize = astrs.size();211int index = rn.nextInt(arrSize);212StringAndId item = astrs.get(index);213int n = rn.nextInt(UNIQUE_STRINGS);214item.str = "Unique String " + n;215item.id = n;216217if (loop++ % 1000 == 0) {218// enough GC cycles for rewritten strings to be deduplicated219if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {220break;221}222223// enough time is spent waiting for GC to happen224if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {225break;226}227}228}229verifyDedupString(astrs);230}231}232233234